jdbc prepareStatement 操作

xiaoxiao2021-02-28  75

package com.wisely.ch5_2_2; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; import com.mysql.jdbc.Connection; public class Test { private static Connection getConn() { String driver = "com.mysql.jdbc.Driver"; String url ="jdbc:mysql://127.0.0.1:3306/my"; String username = "root"; String password = "root"; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static void main(String[]args){ Connection con=getConn(); String sql="insert into user (name,sex) values (?,?)"; String sql1="select sex from user "; try { PreparedStatement ps=con.prepareStatement(sql); ps.setString(1, "1"); ps.setString(2, "b"); //查询并显示 /*ResultSet res=ps.executeQuery(); int col = res.getMetaData().getColumnCount(); while(res.next()) { System.out.println(res.getString(1)); }*/ //关闭自动提交增加插入速度 con.setAutoCommit(false); long time=System.currentTimeMillis(); for(int i=0;i<10000;i++) { ps.executeUpdate(); } con.commit(); long end=System.currentTimeMillis(); System.out.println(end-time); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
转载请注明原文地址: https://www.6miu.com/read-20621.html

最新回复(0)