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 都是 字符型吗? 怎么都用 单引号 括起来。