1

When I write a name as a user, I need to access the surname for this name which is already in database. For example:

Enter a name: beste

beste's surname is: ozcaglar

When I execute my code I can't see any surname as output.

In my database, I have name, surname and no (Auto-Incremented) columns.

import java.sql.*;
import java.util.*;

public class ConnectionMySQL {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a name: ");
        String isim = scan.next();

        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/student","root","");
            //System.out.println("Connection success");
            String query= "SELECT surname FROM student_table WHERE name='isim'";
            Statement stm =conn.createStatement();
            ResultSet rs= stm.executeQuery(query);

            while (rs.next()) {
                System.out.println("Name: " + rs.getString("name")+ " Surname: "+rs.getString("surname"));
            }
        }
        catch (Exception e) {
            System.err.println(e);
        }
    }
}

2 Answers 2

1

You can follow this:

import java.sql.*;
import java.util.*;
public class test4 {
public static void main(String[] args) {
    @SuppressWarnings("resource")
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a name: ");
    String isim = scan.next();
    try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
            System.out.println("Connection success");
            String query = "SELECT * FROM users WHERE surname='" + isim + "'";
            Statement stm = conn.createStatement();
            ResultSet rs = stm.executeQuery(query);
            while (rs.next()) {
                String fName = rs.getString(1);
                String sName = rs.getString(2);
                System.out.println("Name: " + fName + " Surname: " + sName);
            }
        } catch (Exception e) {
        System.err.println("Error");
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes, it works but query should be like this : String query = "SELECT * FROM users WHERE name='" + isim + "'"...Also, thank you for your answer too !
I think it's correct according to the table I created. Sorry I created my table with different name. Thanks
0

The error means that the table doesn't have a column that is called "isim".

Maybe you meant to write:

SELECT surname FROM student_table WHERE name='isim'

?

EDIT (following the comments):

query = "SELECT surname FROM student_table WHERE name='" + isim + "'"

However this should not be used in a real-world application for security reasons (it allows SQL injection attacks).

In actual production code you should either escape input strings, or use parameterized queries...

12 Comments

Oh, yes thank you I didn't realise it. But when I wrote it like WHERE name='isim', I coulnd't reach see any result.
Are you sure you have a row that fits this criteria in the DB?
Yes. In the DB, I have "beste" as name and "ozcaglar" as surname.
but what's isimthen if its not the name you are searching??? Your query should be WHERE name='beste'
Oh I see.. you want to add the content of a variable called "isim". Maybe like this - " ... WHERE name='" + isim + "'"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.