I have an application with a login form. What I want is simply retrieve the credentials of the user who logs in, ie select the username and password from the login form and make a simple select query. However the resultset always return null, no matter what changes I made. I would like to ask what could be the problem? For the purpose of testing I have made 2 static variable in which I store the retrieved username and password (I will made the other alongside these fields in 1 class User but after I fix this). Here is the code: /The fields in the database are exactly like the ones written in the brackets of the getString() method/
package graphical_interface;
import business_logic.User;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Controller {
@FXML
public Button loginB;
public Button exitB;
public TextField userinp;
public TextField passinp;
public Parent pr;
public static String s1 = null;
public static String s2 = null;
@FXML
private void closeApp() {
Platform.exit();
}
String username = null;
String password = null;
@FXML
private void checkB(){
try {
PreparedStatement pstmt2 = null;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://remotemysql.com/CXcocPWj6l", "CXcocPWj6l", "czNrEV9umD");
String query = "select * from User where Username = ? and PassWord = ?";
pstmt2.setString(1,userinp.getText());
pstmt2.setString(2,passinp.getText());
ResultSet valueExist = pstmt2.executeQuery(query);
if(valueExist.next()){
s1 = valueExist.getString("Username");
s2 = valueExist.getString("PassWord");
// System.out.println(valueExist.getString("PassWord");
}
conn.close();
System.out.println(s1);
System.out.println(s2);
} catch (Exception exp){
System.out.println(exp.getLocalizedMessage());
}
}
}
The SQL syntax seems to be correct, so few questions: are you sure that the table contains the record you are finding for? Are you sure that "userinp.getText()" and "passinp.getText()" return something not null?
Finally two advice: use try with resource or at least use the finally block.
Replace the columnLabel with columnIndex ie replace
if(valueExist.next()){
s1 = valueExist.getString("Username");
s2 = valueExist.getString("PassWord");
}
with
if(valueExist!=null && valueExist.next()){
s1 = valueExist.getString(columnIndex-of-Username);
s2 = valueExist.getString(columnIndex-of-PassWord);
}
where columnIndex-of-Username
and columnIndex-of-PassWord
are integer values holding the columnIndex of Username
and PassWord
respectively.
Note : the first column is 1, the second is 2, ...
If you have any typo in columnLabels, it may help fix your problem. Using columnIndex
instead of columnLabel
has another advantage of performance improvement (check https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzaha/jdbcperf.htm )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.