public
static
void
main(String[] args)
{
Class.forName("sqldriver");
Connection conn = DriverManager.getConnection(URL,USER,PASS)
//无参,添加案例
Statement stmt = conn.createStatement();
stmt.addBatch("insert into authors(firstName,lastName) values('fegor','hack')");
stmt.addBatch("insert into authors(firstName,lastName) values('fegors','hacks')");
stmt.addBatch("insert into authors(firstName,lastName) values('fegorsr','hacksr')");
stmt.executeBatch();
stmt.close();
conn.close();
//带参,添加案例
PreparedStatement ps=conn.prepareStatement("insert into authors(firstName,lastName) values(?,?)");
ps.setString(1,"nihao1");//设置参数
ps.setString(2,"wohao1");
ps.addBatch(); //把语句加入批处理队列
ps.setString(1,"nihao2");
ps.setString(2,"wohao2");
ps.addBatch();
ps.setString(1,"nihao3");
ps.setString(2,"wohao3");
ps.addBatch();
ps.executeBatch(); //执行批处理
ps.close(); //最后关闭
conn.close();
}
//
总结:
//
1.stmt=conn.createStatement()这个后面的括号里面一般没有东西!!!
//
2.pstmt=conn.prepareStament("insert into authors(firstName,lastName) values(?,?)")
//
这个后面一定有sql语句,并且里面的参数是用?表示的。
//
然后用pstmt.setString(1,"fegor")来设定它的值,1表示第1个问号的位置,"fegor",表示我们要加的值
//
最后用pstmt.executeUpdate()去执行。
Class.forName("sqldriver");
Connection conn = DriverManager.getConnection(URL,USER,PASS)
//无参,添加案例
Statement stmt = conn.createStatement();
stmt.addBatch("insert into authors(firstName,lastName) values('fegor','hack')");
stmt.addBatch("insert into authors(firstName,lastName) values('fegors','hacks')");
stmt.addBatch("insert into authors(firstName,lastName) values('fegorsr','hacksr')");
stmt.executeBatch();
stmt.close();
conn.close();
//带参,添加案例
PreparedStatement ps=conn.prepareStatement("insert into authors(firstName,lastName) values(?,?)");
ps.setString(1,"nihao1");//设置参数
ps.setString(2,"wohao1");
ps.addBatch(); //把语句加入批处理队列
ps.setString(1,"nihao2");
ps.setString(2,"wohao2");
ps.addBatch();
ps.setString(1,"nihao3");
ps.setString(2,"wohao3");
ps.addBatch();
ps.executeBatch(); //执行批处理
ps.close(); //最后关闭
conn.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)