I'm trying to implement a payroll system and got some issues with it. The program needs to be able retrieve data from mysql db to jtextfield.
我正在尝试实施薪资系统并遇到一些问题。该程序需要能够从mysql db检索数据到jtextfield。
when I try to search I get and error saying "com.mysql.jdbc.exception.jdbc4.MySQLSyntaxErrorException. You have an error in your SQL systax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1"
当我尝试搜索时,我得到并且错误地说“com.mysql.jdbc.exception.jdbc4.MySQLSyntaxErrorException。您的SQL系统中有错误;请查看与您的MySQL服务器版本对应的手册,以便在正确的语法附近使用' ?在第1行“
package AppPackage;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AllowanceGUI extends javax.swing.JFrame {
Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;
public AllowanceGUI() {
initComponents();
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
conn=MySQLConnect.ConnectDb();
}
private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "SELECT EmployeeID,FirstName,LastName,Designation,BasicSalary FROM EmployeeInfo WHERE EmployeeID =?";
pst=conn.prepareStatement(sql);
pst.setString(1,EmployeeIDSearchField.getText());
rs = pst.executeQuery(sql);
while(rs.next()) {
EmployeeIDField.setText(rs.getString("EmployeeID"));
FirstNameField.setText(rs.getString("FirstName"));
LasNameField.setText(rs.getString("LastName"));
DesignationField.setText(rs.getString("Designation"));
BasicSalaryField.setText(rs.getString("BasicSalary"));
}
} catch (SQLException e ) {
JOptionPane.showMessageDialog(null, e);
}}
my db connection is as below;
我的数据库连接如下;
package AppPackage;
import java.sql.*;
import javax.swing.*;
public class MySQLConnect {
Connection conn = null;
public static Connection ConnectDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://Localhost/easypay","root","");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
can anybody please help me with this code?
任何人都可以帮我这个代码吗?
2 个解决方案
#1
you are doing
你在干嘛
pst=conn.prepareStatement(sql);// Prepare this sql query for me
pst.setString(1,EmployeeIDSearchField.getText());// Attach this value as first parameter
rs = pst.executeQuery(sql);// IGNORE EVERYTHING, Execute this query
you should do pst.executeQuery();
on the third line instead.
你应该做pst.executeQuery();而在第三行。
EDIT
Totally read over the fact it was a select. you should just do executeQuery without the string query. P.s. for greater speed use column indexes(integers) instead of column names in your rs.next() loop.
完全阅读了它是一个选择的事实。你应该在没有字符串查询的情况下执行executeQuery。附:为了更快的速度,使用列索引(整数)而不是rs.next()循环中的列名。
#2
Finally found the error.
终于找到了错误。
private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "SELECT EmployeeID,FirstName,LastName,Designation,BasicSalary FROM EmployeeInfo WHERE EmployeeID =?";
pst=conn.prepareStatement(sql);
pst.setString(1,EmployeeIDSearchField.getText());
rs = pst.executeQuery();
if(rs.next()) {
String ID = rs.getString("EmployeeID");
EmployeeIDField.setText(ID);
String FN = rs.getString("FirstName");
FirstNameField.setText(FN);
String LN = rs.getString("LastName");
LasNameField.setText(LN);
String Des = rs.getString("Designation");
DesignationField.setText(Des);
String BS = rs.getString("BasicSalary");
BasicSalaryField.setText(BS);
}
} catch (SQLException e ) {
JOptionPane.showMessageDialog(null, e);
}
}
#1
you are doing
你在干嘛
pst=conn.prepareStatement(sql);// Prepare this sql query for me
pst.setString(1,EmployeeIDSearchField.getText());// Attach this value as first parameter
rs = pst.executeQuery(sql);// IGNORE EVERYTHING, Execute this query
you should do pst.executeQuery();
on the third line instead.
你应该做pst.executeQuery();而在第三行。
EDIT
Totally read over the fact it was a select. you should just do executeQuery without the string query. P.s. for greater speed use column indexes(integers) instead of column names in your rs.next() loop.
完全阅读了它是一个选择的事实。你应该在没有字符串查询的情况下执行executeQuery。附:为了更快的速度,使用列索引(整数)而不是rs.next()循环中的列名。
#2
Finally found the error.
终于找到了错误。
private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "SELECT EmployeeID,FirstName,LastName,Designation,BasicSalary FROM EmployeeInfo WHERE EmployeeID =?";
pst=conn.prepareStatement(sql);
pst.setString(1,EmployeeIDSearchField.getText());
rs = pst.executeQuery();
if(rs.next()) {
String ID = rs.getString("EmployeeID");
EmployeeIDField.setText(ID);
String FN = rs.getString("FirstName");
FirstNameField.setText(FN);
String LN = rs.getString("LastName");
LasNameField.setText(LN);
String Des = rs.getString("Designation");
DesignationField.setText(Des);
String BS = rs.getString("BasicSalary");
BasicSalaryField.setText(BS);
}
} catch (SQLException e ) {
JOptionPane.showMessageDialog(null, e);
}
}