jdbc 3

package com.myivtec.type4.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

import junit.framework.TestCase;

import com.myivtec.type4.util.DB;

public class PrepareadeStatementTest extends TestCase {
    private Connection con;
    private Statement stmt;
    private PreparedStatement pstmt;
    private ResultSet rs;
    public void testQuery() {
        try {
            con = DB.getConnection();
            pstmt = con.prepareStatement("select * from user where id =? and username = ?");//?占位符
            Timestamp time = new Timestamp(new java.util.Date().getTime());
            pstmt.setInt(1, 1208);   //1代表Id
            pstmt.setString(2,"1187");
            //pstmt.setTimestamp(3, time);
            rs = pstmt.executeQuery();
            rs.next();
            System.out.println(rs.getInt(1)+rs.getString(2)+rs.getString(6));
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                pstmt.close();
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public void testInsert() {
        try {
            con = DB.getConnection();
            String sql = "insert into user values(null,?,?,?,?,now())";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1,"o1");
            pstmt.setString(2, "o1");
            pstmt.setInt(3, 20);
            pstmt.setString(4, "m");
            pstmt.addBatch();//加入批处理
            
            pstmt.setString(1,"o2");
            pstmt.setString(2, "o2");
            pstmt.setInt(3, 22);
            pstmt.setString(4, "f");
            pstmt.addBatch();
            
            pstmt.setString(1,"o3");
            pstmt.setString(2, "o3");
            pstmt.setInt(3, 23);
            pstmt.setString(4, "f");
            pstmt.addBatch();
            
            pstmt.executeBatch();//int[]
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                pstmt.close();
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public void testUpdate() throws Exception {
        con = DB.getConnection();
        pstmt = con.prepareStatement("update user set username=?,password=? where Id=?");
        pstmt.setString(1,"郭欣");
        pstmt.setString(2, "123");
        pstmt.setInt(3,36);
        
        pstmt.executeUpdate();
        pstmt.close();
        con.close();
    }
}
DB.java

package
com.myivtec.type4.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class DB { private static String driverClassName; private static String url; private static String user; private static String password; private static final Properties p = new Properties(); static { try { p.load(new FileInputStream("pro.properties")); driverClassName = p.getProperty("driverClassName"); url = p.getProperty("url"); Class.forName(driverClassName); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() throws SQLException { Connection con = null; con = DriverManager.getConnection(url,p); return con; } }
package com.myivtec.type4.test;

import java.sql.Connection;
import java.sql.Statement;

import com.myivtec.type4.util.DB;

public class BatchDemo {
    public static void main(String[] args) throws Exception {
        Connection con = DB.getConnection();
        Statement stmt = con.createStatement();
        for(int i=0;i<10000;i++) {
            String sql = "insert into user values(null,'"+i+"','"+i+"',"+i+",null,now())";
            //insert into user values(null,'10','10',10,null,now());
            stmt.executeUpdate(sql);
        }
        stmt.close();
        con.close();
    }

}
package com.myivtec.type4.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.myivtec.type4.util.DB;


public class BatchDemo2 {

    public static void main(String[] args) throws Exception {
        Connection con = DB.getConnection();
        String sql = "insert into user values(null,?,?,?,?,now())";
        PreparedStatement pstmt = con.prepareStatement(sql);
        for(int i=0;i<10007;i++) {
            pstmt.setString(1,i+"");
            pstmt.setString(2,i+"");
            pstmt.setInt(3, i);
            pstmt.setString(4,"男");
            pstmt.addBatch();
            if(i%50==0) {
                pstmt.executeBatch();
                pstmt.clearBatch();
            }
        }
        pstmt.executeBatch();//**
        pstmt.clearBatch();//**
        pstmt.close();
        con.close();
    }

}

 

posted on 2013-12-03 19:50  weiguoyuan  阅读(187)  评论(0)    收藏  举报

导航