I get a parameter is called ‘id’ in my function and want to print the cell of the name of this id row.
for example:
this is my table:
id name email
1 alon alon@gmail.com
I send to my function: func(1), so I want it to print ‘alon’.
this is what I tried:
static final String url = "jdbc:mysql://localhost:3306/database_alon";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, "root", "Admin");
String query_txt = "SELECT * FROM authors WHERE id = " + id;
Statement ps2 = con.createStatement();
ResultSet my_rs = ps2.executeQuery(query_txt);
System.out.println(my_rs.getString("name"));
con.close;
Everything is fine, but just one problem. You need to move your
ResultSetcursor to the first row before fetching any values: –Use: –
As a side note, consider using
PreparedStatementto avoid getting attacked by SQL Injection.Here’s how you use it: –