create table user( id int primary key auto_increment, username varchar(20) unique not null, password varchar(20) not null, email varchar(40) not null ); INSERT INTO USER VALUES(NULL,'tom','123','[email protected]'); INSERT INTO USER VALUES(NULL,'fox','123','[email protected]');编程从user表中读取数据,并打印在命令行窗口中。
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mysql.jdbc.Driver; public class JdbcDemo1 { public static void main(String[] args) throws SQLException { // 1.注册驱动 DriverManager.registerDriver(new Driver()); // 2.获取连接对象 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb1", "root", "root"); // 3.通过连接对象获取操作sql语句Statement Statement st = con.createStatement(); // 4.操作sql语句 String sql = "select * from user"; // 操作sql语句(select语句),会得到一个ResultSet结果集 ResultSet rs = st.executeQuery(sql); // 5.遍历结果集 // boolean flag = rs.next(); // 向下移动,返回值为true,代表有下一条记录. // int id = rs.getInt("id"); // String username=rs.getString("username"); // System.out.println(id); // System.out.println(username); while(rs.next()){ int id=rs.getInt("id"); String username=rs.getString("username"); String password=rs.getString("password"); String email=rs.getString("email"); System.out.println(id+" "+username+" "+password+" "+email); } //6.释放资源 rs.close(); st.close(); con.close(); } }
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //解决关于加载驱动问题 public class JdbcDemo2 { public static void main(String[] args) throws SQLException, ClassNotFoundException { // 1.注册驱动 // DriverManager.registerDriver(new Driver()); //加载了两个驱动 Class.forName("com.mysql.jdbc.Driver"); // 加载mysql驱动 Class.forName("oracle.jdbc.driver.OracleDriver");// 加载oracle驱动 // String url="jdbc:mysql://localhost:3306/day17"; String url = "jdbc:mysql:///mydb1"; // 2.获取连接对象 Connection con = DriverManager.getConnection(url, "root", "root"); // 3.通过连接对象获取操作sql语句Statement Statement st = con.createStatement(); // 4.操作sql语句 String sql = "select * from user"; // 操作sql语句(select语句),会得到一个ResultSet结果集 ResultSet rs = st.executeQuery(sql); // 5.遍历结果集 // boolean flag = rs.next(); // 向下移动,返回值为true,代表有下一条记录. // int id = rs.getInt("id"); // String username=rs.getString("username"); // System.out.println(id); // System.out.println(username); while (rs.next()) { int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " " + username + " " + password + " " + email); } // 6.释放资源 rs.close(); st.close(); con.close(); } }
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //演示executeUpdate方法 public class JdbcDemo3 { public static void main(String[] args) throws SQLException, ClassNotFoundException { // 1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 加载mysql驱动 String url = "jdbc:mysql:///mydb1"; // 2.获取连接对象 Connection con = DriverManager.getConnection(url, "root", "root"); // 3.通过连接对象获取操作sql语句Statement Statement st = con.createStatement(); // 4.执行update语句 int row = st.executeUpdate("update user set password='135' where id=1"); System.out.println(row); // 6.释放资源 st.close(); con.close(); } }
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //ResultSet介绍 public class JdbcDemo4 { public static void main(String[] args) throws SQLException, ClassNotFoundException { // 1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 加载mysql驱动 String url = "jdbc:mysql:///mydb1"; // 2.获取连接对象 Connection con = DriverManager.getConnection(url, "root", "root"); // 3.通过连接对象获取操作sql语句Statement Statement st = con.createStatement(); // 4.操作sql语句 String sql = "select * from user"; ResultSet rs = st.executeQuery(sql); // 5.遍历结果集 while (rs.next()) { String id = rs.getString("id"); String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " " + username + " " + password + " " + email); } // 6.释放资源 rs.close(); st.close(); con.close(); } }
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //对异常进行try-catch public class JdbcDemo5 { public static void main(String[] args) { Connection con = null; Statement st = null; ResultSet rs = null; try { // 1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 con = DriverManager.getConnection("jdbc:mysql:///mydb1", "root", "root"); // 3.获取操作sql语句对象Statement st = con.createStatement(); // 4.执行sql rs = st.executeQuery("select * from user"); // 5.遍历结果集 while (rs.next()) { int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " " + username + " " + password + " " + email); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 6.释放资源 try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JdbcUtils { public static Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 Connection con = DriverManager.getConnection("jdbc:mysql:///day17", "root", "abc"); return con; } }关于上述的抽取JdbcUtils的缺点:
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; //使用配置文件 public class JdbcUtils { private static final String DRIVERCLASS; private static final String URL; private static final String USERNAME; private static final String PASSWORD; static { DRIVERCLASS = ResourceBundle.getBundle("jdbc").getString("driverClass"); URL = ResourceBundle.getBundle("jdbc").getString("url"); USERNAME = ResourceBundle.getBundle("jdbc").getString("username"); PASSWORD = ResourceBundle.getBundle("jdbc").getString("password"); } static { try { // 将加载驱动操作,放置在静态代码块中.这样就保证了只加载一次. Class.forName(DRIVERCLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { // 2.获取连接 Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD); return con; } //关闭操作 public static void closeConnection(Connection con) throws SQLException{ if(con!=null){ con.close(); } } public static void closeStatement(Statement st) throws SQLException{ if(st!=null){ st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException{ if(rs!=null){ rs.close(); } } }测试CURD操作,并测试工具类、
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; import cn.itcast.utils.JdbcUtils; import cn.itcast.utils.JdbcUtils1; //jdbc的crud操作 public class JdbcDemo6 { @Test public void findByIdTest() { // 1.定义sql String sql = "select * from user where id=1"; Connection con = null; Statement st = null; ResultSet rs = null; try { // 1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 con = DriverManager.getConnection("jdbc:mysql:///mydb1", "root", "root"); // 3.获取操作sql语句对象Statement st = con.createStatement(); // 4.执行sql rs = st.executeQuery(sql); // 5.遍历结果集 while (rs.next()) { int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " " + username + " " + password + " " + email); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 6.释放资源 try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } // 添加操作 @Test public void addTest() { // 定义sql String sql = "insert into user values(null,'张三','123','[email protected]')"; Connection con = null; Statement st = null; ResultSet rs = null; try { // 1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 con = DriverManager.getConnection("jdbc:mysql:///day17", "root", "abc"); // 3.获取操作sql语句对象Statement st = con.createStatement(); // 4.执行sql int row = st.executeUpdate(sql); if (row != 0) { System.out.println("添加成功"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 6.释放资源 try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } // update操作 @Test public void updateTest() { // 将id=3的人的password修改为456 String password = "456"; String sql = "update user set password='" + password + "' where id=3"; // 1.得到Connection Connection con = null; Statement st = null; try { con = JdbcUtils1.getConnection(); // 3.获取操作sql语句对象Statement st = con.createStatement(); // 4.执行sql int row = st.executeUpdate(sql); if (row != 0) { System.out.println("修改成功"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } // delete测试 @Test public void deleteTest() { // 将id=3的人删除 String sql = "delete from user where id=2"; // 1.得到Connection Connection con = null; Statement st = null; try { con = JdbcUtils.getConnection(); // 3.获取操作sql语句对象Statement st = con.createStatement(); // 4.执行sql int row = st.executeUpdate(sql); if (row != 0) { System.out.println("删除成功"); } } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { JdbcUtils.closeStatement(st); JdbcUtils.closeConnection(con); } catch (SQLException e) { e.printStackTrace(); } } } }
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; //滚动结果集 public class JdbcDemo7 { public static void main(String[] args) throws SQLException { // 1.得到一个Connection Connection con = JdbcUtils.getConnection(); // 2.得到一个执行sql语句Statement Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // 3.执行sql,得到结果集 ResultSet rs = st.executeQuery("select * from user"); // 4.定位 rs.absolute(3); // System.out.println(rs.getInt("id") + " " + // rs.getString("username")); // rs.afterLast(); // // while (rs.previous()) { // System.out.println(rs.getInt("id") + " " // + rs.getString("username")); // } rs.updateString("password", "456"); rs.updateRow(); // 5.关闭 rs.close(); st.close(); con.close(); } }
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql:///mydb1 username=root password=root #driverClass=oracle.jdbc.driver.OracleDriver #url=jdbc:oracle:thin:@localhost:1521:XE #username=system #password=systemUserDao.java
package cn.itcast.dao; import cn.itcast.domain.User; public interface UserDao { public User findUser(User user) throws Exception; }UserDaoImpl.java
package cn.itcast.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.domain.User; import cn.itcast.exception.LoginException; import cn.itcast.utils.JdbcUtils; public class UserDaoImpl implements UserDao{ // 查找用户---使用Statement完成登录操作,存在风险(sql注入) public User _findUser(User user) throws SQLException { // 1.sql语句 String sql = "select * from user where username='" + user.getUsername() + "' and password='" + user.getPassword() + "'"; // 2.执行sql Connection con = null; Statement st = null; ResultSet rs = null; try { con = JdbcUtils.getConnection(); st = con.createStatement(); rs = st.executeQuery(sql); if (rs.next()) { // 如果可以next,代表查找到了这个用户的信息,就将结果集中的信息封装到User对象中. User u = new User(); u.setId(rs.getInt("id")); u.setUsername(rs.getString("username")); u.setPassword(rs.getString("password")); u.setEmail(rs.getString("email")); return u; } } finally { try { JdbcUtils.closeResultSet(rs); JdbcUtils.closeStatement(st); JdbcUtils.closeConnection(con); } catch (SQLException e) { e.printStackTrace(); } } return null; } // 使用PreparedStatement来完成操作,它可以解决sql注入. public User findUser(User user) throws SQLException { // 1.sql语句 String sql = "select * from user where username=? and password=?"; // 2.执行sql Connection con = null; PreparedStatement pst = null; ResultSet rs = null; try { con = JdbcUtils.getConnection(); pst = con.prepareStatement(sql); pst.setString(1, user.getUsername()); pst.setString(2, user.getPassword()); rs = pst.executeQuery();// 无参数 if (rs.next()) { // 如果可以next,代表查找到了这个用户的信息,就将结果集中的信息封装到User对象中. User u = new User(); u.setId(rs.getInt("id")); u.setUsername(rs.getString("username")); u.setPassword(rs.getString("password")); u.setEmail(rs.getString("email")); return u; } }finally { try { JdbcUtils.closeResultSet(rs); JdbcUtils.closeStatement(pst); JdbcUtils.closeConnection(con); } catch (SQLException e) { e.printStackTrace(); } } return null; } }User.java
package cn.itcast.domain; public class User { private int id; private String username; private String password; private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }LoginException.java
package cn.itcast.exception; public class LoginException extends Exception { public LoginException() { super(); } public LoginException(String message, Throwable cause) { super(message, cause); } public LoginException(String message) { super(message); } public LoginException(Throwable cause) { super(cause); } }UserService.java
package cn.itcast.service; import java.sql.SQLException; import cn.itcast.dao.UserDaoImpl; import cn.itcast.domain.User; import cn.itcast.exception.LoginException; public class UserService { // service层的登录方法 public User login(User user) throws LoginException { User existUser = null; try { existUser = new UserDaoImpl().findUser(user); } catch (SQLException e) { e.printStackTrace(); throw new LoginException("登录失败"); } return existUser; } }JdbcUtils.java
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; //使用配置文件 public class JdbcUtils { private static final String DRIVERCLASS; private static final String URL; private static final String USERNAME; private static final String PASSWORD; static { DRIVERCLASS = ResourceBundle.getBundle("jdbc").getString("driverClass"); URL = ResourceBundle.getBundle("jdbc").getString("url"); USERNAME = ResourceBundle.getBundle("jdbc").getString("username"); PASSWORD = ResourceBundle.getBundle("jdbc").getString("password"); } static { try { // 将加载驱动操作,放置在静态代码块中.这样就保证了只加载一次. Class.forName(DRIVERCLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { // 2.获取连接 Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD); return con; } //关闭操作 public static void closeConnection(Connection con) throws SQLException{ if(con!=null){ con.close(); } } public static void closeStatement(Statement st) throws SQLException{ if(st!=null){ st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException{ if(rs!=null){ rs.close(); } } }LoginServlet.java
package cn.itcast.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.domain.User; import cn.itcast.exception.LoginException; import cn.itcast.service.UserService; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.得到用户名与密码 String username = request.getParameter("username"); String password = request.getParameter("password"); // 2.封装属性到javaBean User user = new User(); user.setUsername(username); user.setPassword(password); // 3.调用service中登录方法 UserService service = new UserService(); User existUser = null; try { existUser=service.login(user); if (existUser == null) { // 代表用户名或密码错误,存储错误信息在request域,请求转发到login.jsp request.setAttribute("login.message", "用户名或密码错误"); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } else { request.getSession().setAttribute("user", existUser); response.sendRedirect(request.getContextPath() + "/success.jsp"); return; } } catch (LoginException e) { request.setAttribute("login.message", e.getMessage()); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> ${requestScope["login.message"]}<br> <form action="${pageContext.request.contextPath}/login" method="post"> username:<input type="text" name="username"><br> password:<input type="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body>登录成功:${user.username} </body> </html>
(一)使用JDBC处理大文本:
package cn.itcast.lob; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import cn.itcast.utils.JdbcUtils; public class MyTextTest { // 存储 @Test public void save() throws SQLException, FileNotFoundException { String sql = "insert into mytext values(null,?)"; // 1.获取Connection Connection con = JdbcUtils.getConnection(); // 2.获取PreparedStatement PreparedStatement pst = con.prepareStatement(sql); // 3.插入值 File file = new File("F:\\myEclipse_project\\day17_3\\a.txt"); FileReader fr = new FileReader(file); pst.setCharacterStream(1, fr, (int) (file.length())); pst.executeUpdate(); // 4.释放资源 pst.close(); con.close(); } // 获取 @Test public void get() throws SQLException, IOException { String sql = "select * from mytext where id=?"; // 1.获取Connection Connection con = JdbcUtils.getConnection(); // 2.获取PreparedStatement PreparedStatement pst = con.prepareStatement(sql); pst.setInt(1, 1); // 3.得到结果集 ResultSet rs = pst.executeQuery(); // 4.遍历结果集 if (rs.next()) { Reader r = rs.getCharacterStream("content"); FileWriter fw = new FileWriter("d:/笔记.txt"); int len = -1; char[] ch = new char[1024 * 100]; while ((len = r.read(ch)) != -1) { fw.write(ch, 0, len); fw.flush(); } fw.close(); r.close(); } pst.close(); con.close(); } }
package cn.itcast.lob; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import cn.itcast.utils.JdbcUtils; public class MyBlobTest { // 添加 @Test public void save() throws SQLException, IOException { String sql = "insert into myblob values(null,?)"; // 1.获取Connection Connection con = JdbcUtils.getConnection(); // 2.获取PreparedStatement PreparedStatement pst = con.prepareStatement(sql); // 3.插入值 File file = new File("C:\\Users\\lx\\Desktop\\2.jdbc介绍.avi"); FileInputStream fis = new FileInputStream(file); pst.setBinaryStream(1, fis, (int) (file.length())); int row = pst.executeUpdate(); if (row != 0) { System.out.println("插入成功"); } // 4.释放资源 fis.close(); pst.close(); con.close(); } // 获取 @Test public void get() throws SQLException, IOException { String sql = "select * from myblob where id=?"; // 1.获取Connection Connection con = JdbcUtils.getConnection(); // 2.获取PreparedStatement PreparedStatement pst = con.prepareStatement(sql); pst.setInt(1, 1); // 3.得到结果集 ResultSet rs = pst.executeQuery(); // 4.遍历结果集 if (rs.next()) { // System.out.println(rs.getInt("id")); InputStream is = rs.getBinaryStream("content");// 得到的这个输入流它的源可以理解成就是数据库中的大二进制信息 FileOutputStream fos = new FileOutputStream("d:/a.avi"); int len = -1; byte[] b = new byte[1024 * 100]; while ((len = is.read(b)) != -1) { fos.write(b, 0, len); fos.flush(); } fos.close(); is.close(); } // 5.关闭 rs.close(); pst.close(); con.close(); } }存入视频:
package cn.itcast.batch; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; public class StatementBatchTest { public static void main(String[] args) throws SQLException { // 定义sql语句 String sql1 = "create table person(id int,name varchar(20))"; String sql2 = "insert into person values(1,'tom')"; String sql3 = "insert into person values(2,'fox')"; String sql4 = "insert into person values(3,'tony')"; String sql5 = "update person set name='张三' where id=1"; String sql6 = "delete from person where id=3"; Connection con = JdbcUtils.getConnection(); // 得到一个Statement对象 Statement st = con.createStatement(); // 使用批处理执行sql st.addBatch(sql1); st.addBatch(sql2); st.addBatch(sql3); st.addBatch(sql4); st.addBatch(sql5); st.addBatch(sql6); // 执行批处理 st.executeBatch(); st.close(); con.close(); } }
package cn.itcast.batch; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import cn.itcast.utils.JdbcUtils; public class PreparedStatementBatchTest { public static void main(String[] args) throws SQLException { // 向person表中插入1000条数据 String sql = "insert into person values(?,?)"; Connection con = JdbcUtils.getConnection(); PreparedStatement pst = con.prepareStatement(sql); // 批处理 long l=System.currentTimeMillis(); for (int i = 1; i <= 100000; i++) { pst.setInt(1, i); pst.setString(2, "name" + i); pst.addBatch(); if(i%1000==0){ pst.executeBatch(); pst.clearBatch(); //清空缓存。 } } //执行批处理 pst.executeBatch(); pst.close(); con.close(); System.out.println(System.currentTimeMillis()-l); } }