jdbc批处理操作
1.什么是jdbc批处理操作?
就是有许多sql语句:一起执行
stmt.addBatch();//放入sql语句(一起执行)
stmt.executeBatch(); //执行:放入sql语句所有
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
stmt.addBatch(sql4);
我调用了:工具类jdbc:所以:jdbc驱动,连接都实现了
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class pouit {
public static void main(String[] args) throws SQLException {
Connection conn=TYBU.getConnection();
Statement stmt=conn.createStatement();
String sql1="insert into user values('4','我是',776)";
String sql2="insert into user values('4','我是',773)";
String sql3="insert into user values('4','我是',774)";
String sql4="insert into user values('4','我是',775)";
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
stmt.addBatch(sql4);
stmt.executeBatch();
stmt.close();
conn.close();
}
}
效果图:
jdbc:工具类
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class TYBU {
private static final String driverClassName;
private static final String url;
private static final String password;
private static final String usernane;
static {
Properties properties=new Properties();
try {
properties.load(new FileInputStream("D:\\javac\\projects\\jdbc学习\\src\\dp.properties"));
} catch (IOException e) {
e.printStackTrace();
}
driverClassName=properties.getProperty("driverClassName");
url=properties.getProperty("url");
password=properties.getProperty("password");
usernane=properties.getProperty("username");
//url="jdbc:mysql://localhost:3306/new_schema";
//driverClassName="com.mysql.jdbc.Driver";
//password="your-password";
//usernane="root";
}
public static void loadDriver() throws ClassNotFoundException {
try{
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn=null;
try {
loadDriver();
conn= DriverManager.getConnection(url,usernane,password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}}
还使用了:配置文件jdbc