JDBC四(web基础学习笔记十)

一、增加 、修改、删除、查询

将功能整合在一个类中

package pb.base;



import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;



import pb.until.ConfigManager;



public class Base {

    public Connection conn;

    public PreparedStatement pstmt;

    public Statement stmt;

    public ResultSet rs;

    

    //建立连接

    public boolean getConnection(){

        // 获取DRIVER,url,用户名密码

        String driver = ConfigManager.getintance().getvalue("jdbc.driver");

        String url = ConfigManager.getintance().getvalue("jdbc.url");

        String uname = ConfigManager.getintance().getvalue("jdbc.uname");

        String pwd = ConfigManager.getintance().getvalue("jdbc.upwd");

        // (1)使用Class.forName()加载驱动

        try {

            Class.forName(driver);

            // (2)DriverManager.getConnection(URL,用户名,密码)获取数据库的连接Connection

            // Oracle连接方式jdbc:oracle:thin:@localhost:1521:orcl

            conn = DriverManager.getConnection(url, uname, pwd);

        } catch (ClassNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            return false;

        }catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            return false;

        }

        return true;

    }



    //增加,修改,删除

    public int  executeUpdate(String sql,Object [] pararm){

        

        int UpdateRow=0;

        getConnection();

        try {

            pstmt=conn.prepareStatement(sql);

            //遍历数组设置每个值

            for(int i=0;i<pararm.length;i++){

                pstmt.setObject(i+1, pararm[i]);

            }

            UpdateRow = pstmt.executeUpdate();

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return UpdateRow;

    }

    

    

  //查询

    public ResultSet executeSQL(String sql,Object [] pararm){

        getConnection();

        try {

            pstmt=conn.prepareStatement(sql);

            //遍历数组设置每个值

            for(int i=0;i<pararm.length;i++){

                pstmt.setObject(i+1, pararm[i]);

            }

            rs = pstmt.executeQuery();

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return rs;

    }

    

    

    //关闭数据库连接

    public boolean CloseConnection(){

            try {

                if(rs!=null){

                rs.close();

                }

            } catch (SQLException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

                return false;

            }

    

        try {

            if(stmt!=null){

            stmt.close();

            }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            return false;

        }

        try {

            if(pstmt!=null){

            pstmt.close();

            }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            return false;

        }

        try {

            if(conn!=null){

            conn.close();

            }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            return false;

        }

    return true;

        

    }

}

 

你可能感兴趣的:(jdbc)