您的当前位置:首页JDBC连接创建

JDBC连接创建

2024-12-12 来源:哗拓教育

import java.sql

JdbcDemo类


package test;

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 java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

public class JdbcDemo {

    private static Logger logger = Logger.getLogger(JdbcDemo.class.getName());

    Connection conn = null;
    PreparedStatement ps = null;
    Statement stmt = null;
    ResultSet rs = null;
    // 数据库信息
    private String driver = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/epet";
    private String usename = "root";
    private String password = "root";

    /**
     * 获取数据库连接对象
     * 
     * @return
     */
    public Connection getConnection() {

        if (conn == null) {
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, usename, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return conn;
    }

    /**
     * 关闭数据库连接
     * @param conn
     * @param ps
     * @param rs
     */
    public void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (null != ps) {
            try {
                ps.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (null != conn) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 通用增,删,改操作
     * @param sql
     * @param params
     * @return
     */
    public int excuteUpdate(String sql, Object[] params) {// object...params
        conn = this.getConnection();
        int result = 0;
        try {
            ps = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            result = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
        return result;
    }

    /**
     * 通用查询
     * @param sql
     * @return
     */
    public ResultSet excuteQuery(String sql) {
        conn = this.getConnection();
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }

}
显示全文