This is a simple code print some rows from a Database. but When I execute this nothing is print on screen. I figured that the problem is rs.next()
method is skipping a row. So How can I avoid that or Reset the Position of rs.next()
method?
String searchword = Searchtf.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection(url, db_username, db_password);
java.sql.Statement stat = con.createStatement();
String searchQuery = "SELECT * FROM user WHERE Name LIKE '" + searchword + "' ";
java.sql.ResultSet rs = stat.executeQuery(searchQuery);
if (rs.next()) {
while (rs.next()) {
System.out.print(rs.getString("idUser") + " ,");
System.out.print(rs.getString("Name") + " ,");
System.out.print(rs.getString("Email") + " ,");
System.out.println(rs.getString("country") + " .");
}
} else {
JOptionPane.showMessageDialog(null, "Not Found");
}
} catch (Exception ex) {
ex.printStackTrace();
}