我有一个需要Java-Mysql连接的项目。 我一直在尝试通过一个按钮连接两个表,以便在四个JLabel和一个JTextArea中提供输出。
它表明我的SQL语法有错误。
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 = " + "mil@gmail.com" + ";";
String P = "Select INFORMATION FROM pinfo where email = " + "mil@gmail.com" + ";";
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());
}
}
答案 0 :(得分:3)
在WHERE
语句中,必须用单引号'
包围实际值,以引用实际值。 MySQL文档第
9.1.1 String Literals章对此进行了说明。您的代码应为:
String P = "Select INFORMATION FROM pinfo where email = 'mil@gmail.com';";
如果您打算使用JDBC,请了解准备好的状态菜单。参见Using Prepared Statements。
答案 1 :(得分:1)
您错过了电子邮件ID周围的单引号。请尝试以下
String S= "Select mobile,state,mobile,email,job from signup where email = 'mil@gmail.com';";
String P = "Select INFORMATION FROM pinfo where email = 'mil@gmail.com';";