【发布时间】:2016-09-13 13:02:02
【问题描述】:
我可以使用这个在 Netbeans 中正常连接:
Connection conn = null;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/librarymangementsystem","root","root");
if(conn!= null)
{
System.out.println("connected");
}
}catch(Exception e)
{
System.out.println("not connected");
}
}
但是在向列添加数据时,我就是不能。
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/librarymangementsystem","root","root");
Statement stmt=conn.createStatement();
String Query = "INSERT into librarian_details(username, name, email, password) VALUES("+ uname +", "+ fname +", " + emails + ", " + psword +")";
stmt.executeUpdate(Query);
JOptionPane.showMessageDialog(null, "Success!");
}
有人知道这个问题吗? 更新问题:
String Query = "insert into book_details(Book_Name, ISBN, Author, Category, >Quantity, BookShelfNo,Row,Column) VALUES('" +bookname + "','" + ISBN + "','" + > AuthorName + "','" + Category + "','" + 数量 + "','" + ?BookShelfNo +"', '" >+ Row + "', '" + Column + "')";
我似乎无法使用以下方法将任何数据插入行和列:
字符串行 = jTextField9.getText(); 字符串列 = jTextField10.getText(); 行和列数据类型是 int。
【问题讨论】:
-
在您的 catch 语句中,执行
e.printStackTrace()并粘贴您在问题中得到的内容。 -
mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“flores, asdfgh, admin)”附近使用正确的语法。插入时出现此错误。
-
您不应该将值连接到这样的查询字符串中。它是不安全(搜索 SQL 注入),您应该对查询进行参数化并使用
PreparedStatement。