Reputation: 1
I have a project that requires Java-Mysql connectivity. I have been trying to connect two tables from one button to give me an output in four JLabels and a JTextArea.
It shows there is an error in my SQL syntax.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("java.sql.Driver");
Connection con = DriverManager.getConnection("jdbc:Mysql://localhost/nami", "root", "123456");
Statement st = con.createStatement();
String S =
"Select mobile,state,mobile,email,job from signup where email = " + "[email protected]" + ";";
String P = "Select INFORMATION FROM pinfo where email = " + "[email protected]" + ";";
ResultSet rs = st.executeQuery(S);
ResultSet rs2 = st.executeQuery(P);
while (rs.next()) {
String JOB = rs.getString("JOB");
String state = rs.getString("state");
String no = rs.getString("mobile");
String email = rs.getString("email");
jLabel5.setText(JOB);
jLabel16.setText(state);
jLabel31.setText(no);
jLabel7.setText(email);
}
while (rs2.next()) {
String info = rs.getString("information");
jTextArea1.setText(info);
}
con.close();
st.close();
rs.close();
rs2.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error" + e.getMessage());
}
}
Upvotes: 0
Views: 49
Reputation: 1086
You have missed the single quotes around the email id. Try below,
String S= "Select mobile,state,mobile,email,job from signup where email = '[email protected]';";
String P = "Select INFORMATION FROM pinfo where email = '[email protected]';";
Upvotes: 1
Reputation: 44942
In WHERE
statement you must quote the actual value by surrounding it with single quotes '
. This is explained in MySQL docs chapter
9.1.1 String Literals. Your code should be:
String P = "Select INFORMATION FROM pinfo where email = '[email protected]';";
Please learn about the prepared statemenets if you plan to use JDBC. See Using Prepared Statements.
Upvotes: 3