向数据库写入数据的问题
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:SuperMaket";
// String access="D:/数据库/SuperMarket.mdb";
con = DriverManager.getConnection(url,properties);
sql = con.createStatement();
rs = sql.executeQuery("SELECT * FROM GoodsManagement where [GoodsNum] ='"
+ t1.getText() + "'");
if(rs.next()==true)
{
JOptionPane.showMessageDialog(this,"该商品编号已存在\n请重新输入","提示",
JOptionPane.WARNING_MESSAGE);
}else
{
String sqlStr="INSERT INTO GoodsManagement(GoodsNum,GoodsName,GoodsPrice,Production_Date,Production_Place,Inventory)"+
"VALUES('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText()+"','"+
t4.getText()+"','"+t5.getText()+"','"+t6.getText()+")";
// String sqlStr="INSERT INTO GoodsManagement VALUES(t1.getText(),t2.getText(),t3.getText(),t4.getText(),t5.getText(),t6.getText())";
sql.executeUpdate(sqlStr);
JOptionPane.showMessageDialog(this, "添加成功!","添加记录",1);
}
为什么我插入信息时会提示 “ 无效的 SQL语句”,但是我检查不到SQL语句哪里错了 java 数据库 access --------------------编程问答-------------------- statement容易出错,lz用preparedstatement试一下。 --------------------编程问答--------------------
String sqlStr="INSERT INTO GoodsManagement(GoodsNum,GoodsName,GoodsPrice,Production_Date,Production_Place,Inventory)"+
"VALUES('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText()+"','"+
t4.getText()+"','"+t5.getText()+"','"+t6.getText()+")";
我感觉是这句错了,但是我不知道哪里错了,好烦啊! --------------------编程问答-------------------- 把异常代码贴出来 --------------------编程问答-------------------- String sqlStr="INSERT INTO GoodsManagement(GoodsNum,GoodsName,GoodsPrice,Production_Date,Production_Place,Inventory) VALUES(?,?,?,?,?,?) ;
sql = conn.prepareStatement(sqlStr);
sql.setString(1,t1.getText());
sql.setString(2,t1.getText());
sql.setString(3,t1.getText());
sql.setString(4,t1.getText());
sql.setString(5,t1.getText());
sql.setString(6,t1.getText());
sql.executeUpdate();
试一下这个。 --------------------编程问答-------------------- url没写错? --------------------编程问答--------------------
没有 --------------------编程问答--------------------
没有setString()这个方法呀? --------------------编程问答-------------------- 日期类型匹配吗? --------------------编程问答-------------------- 具体错误信息呢。
--------------------编程问答--------------------
String sqlStr="INSERT INTO GoodsManagement(GoodsNum,GoodsName,GoodsPrice,Production_Date,Production_Place,Inventory) VALUES(?,?,?,?,?,?) ;
sql = conn.prepareStatement(sqlStr);
sql.setString(1,t1.getText());
sql.setString(2,t1.getText());
sql.setString(3,t1.getText());
sql.setString(4,t1.getText());
sql.setString(5,t1.getText());
sql.setString(6,t1.getText());
sql.executeUpdate();
试一下这个。
没有setString()这个方法呀?
用PrepareStatement这个,不要用Statement。 --------------------编程问答-------------------- String sqlStr="INSERT INTO GoodsManagement
insert 和into之间的空白可能是tab键或者全角的空格产生的。
补充:Java , Java相关