我正在將 Sql Server 與 Netbeans 一起使用。我正在使用以下代碼執行更新查詢Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //Connection establishment Connection con=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-CU5U75J\\SQLSERVER1;databaseName=SQLConnection","Fateh","Fateh"); //Statement Object Statement st=con.createStatement(); //For DML use executeUpdate method of Statement Class st.executeUpdate("INSERT INTO Emp" +" VALUES("+2+",+Gull)"); //Commit Statement con.commit(); JOptionPane.showMessageDialog(this, "Message", "The Data is Entered", JOptionPane.INFORMATION_MESSAGE); } catch (ClassNotFoundException ex) { Logger.getLogger(Employe.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(Employe.class.getName()).log(Level.SEVERE, null, ex); }}
2 回答

RISEBY
TA貢獻1856條經驗 獲得超5個贊
這條線有問題:
st.executeUpdate("INSERT INTO Emp" +" VALUES("+2+",+Gull)");
更準確地說,字符串連接被搞砸了:
"INSERT INTO Emp"+"NALUES("+2+",+Gull)"
將評估為這樣的字符串:INSERT INTO Emp VALUES(2,+Gull)
。
因為它抱怨Gull
列名無效,所以沒有這樣的列。
所以我懷疑它是一些變量,你想在那里粘貼哪個值。在這種情況下,您可能想嘗試:
"INSERT INTO Emp" + "VALUES(" + 2 + "," + Gull + ")"
這將評估為:
INSERT INTO Emp VALUES(2,[whatever value Gull has])
但是,請記住,如果Gull
是字符串,則必須將其用引號括起來:
"INSERT INTO Emp" + "VALUES(" + 2 + ",'" + Gull + "')"
最后但并非最不重要的是,您很容易受到 SQL 注入的影響!你可能也想考慮一下。
添加回答
舉報
0/150
提交
取消