【发布时间】:2025-11-23 01:10:01
【问题描述】:
我正在尝试在 mysql db 中的表中添加一个新行,我尝试使用 executeUpdate(); 和 executeQuery(); 但两者都不起作用,我从多个 JTextField 中获取列值并添加每个将它们添加到单个 Librarian 对象,然后我在 main 中调用 setLibrarian() 方法。
但我收到以下错误消息:
java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '@gmail.com , 14 , cairo , 4.6565486E7 )' at line 1
这是我的代码:
public static void setLibrarian(Librarian lib){
Connection con = null;
Statement st = null;
String dbURL = "jdbc:mysql://localhost:3306/universitysystem";
String username = "root";
String password = "";
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(dbURL , username , password);
st = (Statement) con.createStatement();
st.executeUpdate("INSERT INTO librarians(username , password , email , address , city , contactno)"
+ " VALUES("+lib.getName()+" , "+lib.getPassword()+" , "+lib.getEmail()+" , "+lib.getAddress()+" , "+lib.getCity()+" , "+lib.getContactNo()+" ); ");
con.close(); //closing connection
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
【问题讨论】: