当前位置:主页 >> Java基础 >> 正文
JDBC操作mysql数据库(SqlHelper类封装)
阅读:672 输入:2017-10-23 18:47:39
package com.vveye;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class sqlHelper {

	 private static Connection conn = null;  
	    private static PreparedStatement ps = null;  
	    private static ResultSet rs = null;  
	    private static CallableStatement cs = null;  
	   
	    private static String driver = "com.mysql.jdbc.Driver";  
	    private static String url = "jdbc:mysql://localhost:3306/cdcol?useUnicode=true&characterEncoding=UTF-8";  
	    private static String userName = "root";  
	    private static String password = "123123";  
	   

	   
	    public static Connection getConn() {  
	        return conn;  
	    }  
	   
	    public static PreparedStatement getPs() {  
	        return ps;  
	    }  
	   
	    public static ResultSet getRs() {  
	        return rs;  
	    }  
	      
	    public static CallableStatement getCs() {  
	        return cs;  
	    }  
	   
	    // 加载驱动,只需要一次  
	    static{
	    try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    }//static
	    // 得到连接  
	    public static Connection getConnection() {  
	        try {
	            conn = DriverManager.getConnection(url, userName, password);  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        }  
	        return conn;  
	    }  
	   
	    // 处理多个update/delete/insert  
	    public static void executeUpdateMultiParams(String[] sql, String[][] parameters) {  
	        try {  
	            // 获得连接  
	            conn = getConnection();  
	            // 可能传多条sql语%