executeupdatesql
(最基本的連接方法)
1。獲取連接
獲取連接需要兩步,
一是使用DriverManager來注冊驅動(Class.forName(「com.mysql.jdbc.Driver」)),二是使用DriverManager來獲取Connection對像DriverManager.getConnection(url,username,password)
2.獲取Statement(Statement stmt =con.createStatement();)
Statement就是執行sql語句的;
3.執行sql語句
String sql = 「insertinto user value(』zhangSan』, 』123』)」;
int m =stmt.executeUpdate(sql);
//總代碼如下
()throwsException{
Class.forName("com.mysql.jdbc.Driver");
Stringurl="jdbc:mysql://localhost:3306/mydb1";
returnDriverManager.getConnection(url,"root","123");
}
@Test
publicvoidinsert()throwsException{
Connectioncon=getConnection();
Statementstmt=con.createStatement();
Stringsql="insertintouservalues('zhangSan','123')";
stmt.executeUpdate(sql);
System.out.println("插入成功!");
}
『貳』 executeUpdate(sql) 返回值是什麼
executeUpdate(sql) 的返回值是一個整數(int)。
當executeUpdate(sql)是INSERT、UPDATE 或 DELETE 語句時,返回的是受影響的行數(即告拍更新的行數)。
當executeUpdate(sql)是CREATE TABLE 或 DROP TABLE 等不操作行的語句,executeUpdate 的返回值是零。
(2)executeupdatesql擴展閱讀
executeUpdate(sql)的用法介紹:
用於執行 INSERT、UPDATE 或 DELETE 語句以及 SQL DDL(數據定義語言)語句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 語句的效果是修改表中零行或多行中的一列或多列。例如:
//載入資料庫驅動
Class.forName("com.mysql.jdbc.Driver");
//使用DriverManager獲取資料庫連接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1234");
//使用Connection來創建一個Statment對象
Statement stmt = conn.createStatement();
//執行DML語句,改友旁返回受影響的記錄條核橡數
return stmt.executeUpdate(sql);
『叄』 stmt.executeUpdate(sql);出錯
你在執行
stmt.executeUpdate(sql);
之前,先把sql語句顯示(列印)出來,到 資料庫中執行一下,看有什麼問題。
t_isin、t_lastMoney 都是 字元型嗎? 怎麼都用 單引號 括起來。