关于Java连接数据库中一次增加多条命令要怎么做?
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;im...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Add
{
public static void main(String[] args) throws ClassNotFoundException {
Connection conn;
Statement stmt = null;
conn = null;
try {
String ur1="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:/java web database/personal information.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(ur1);
stmt = conn.createStatement();
stmt.executeUpdate("insert into base values (3,'小王','XXX','XXX' )" );
//比如我这里还想增加 (4,'小王','XXX','XXX' )" ); (5,'小王','XXX','XXX' )" ); (6,'小王','XXX','XXX' )" );
//我试了在上面加个for循环貌似不行.难道SQL语句只能一句一句增加吗?
System.out.println("成功添加一条数据");
} catch (Exception e) {
System.out.println("sql出错:");
System.out.println(e);
}
finally{
try {
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}
} 展开
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Add
{
public static void main(String[] args) throws ClassNotFoundException {
Connection conn;
Statement stmt = null;
conn = null;
try {
String ur1="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:/java web database/personal information.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(ur1);
stmt = conn.createStatement();
stmt.executeUpdate("insert into base values (3,'小王','XXX','XXX' )" );
//比如我这里还想增加 (4,'小王','XXX','XXX' )" ); (5,'小王','XXX','XXX' )" ); (6,'小王','XXX','XXX' )" );
//我试了在上面加个for循环貌似不行.难道SQL语句只能一句一句增加吗?
System.out.println("成功添加一条数据");
} catch (Exception e) {
System.out.println("sql出错:");
System.out.println(e);
}
finally{
try {
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}
} 展开
展开全部
用PreparedStatement,通过他的addBatch()和executeBatch()方法可以轻松实现批量更睁消新,具悉携知隐伍体代码可参考
http://blog.csdn.net/tower888/article/details/10782781
http://blog.csdn.net/tower888/article/details/10782781
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询