CSDN博客

img fajaven

连接数据库的类

发表于2003/2/8 12:00:00  668人阅读

这是我在网上找到的 CODE 基础上加以修改后自己用来连接数据库的类。

分为三个部分:参数设置,数据库连接,后面还附有应用例子。

如果不能直接应用请与我联系: fajaven@vip.sina.com

/*************************
 * 连接数据库的参数设置
 *************************/

package net.asales.mysql;

public class BeansConstants {
    public static String dbdriver;
    public static String dburl;
   
    private String database = "asales";
    private String usr = "root";
    private String passwd = "";
   
    private BeansConstants(String dbms) {
     //当前只设置了 mysql 的驱动程序
        if (dbms == "mysql") {
            this.dbdriver = "org.gjt.mm.mysql.Driver";
   this.dburl = "jdbc:mysql://localhost/" + this.database + "?useUnicode=true&characterEncoding=gb2312" + "&user=" + this.usr + "&password=" + this.passwd;
        } else {
            this.dbdriver = "none";
        }
    }
   
    public static BeansConstants getInstance(String dbms) {
        BeansConstants bc = new BeansConstants(dbms);
        return bc;
    }
   
    public String getDatabase() { return this.database; }
    public String getUsr() { return this.usr; }
    public String getPasswd() { return this.passwd; }
    public String getDbdriver() { return this.dbdriver; }
    public String getDburl() { return this.dburl; }
   
    public void setDatabase(String database) { this.database = database; }
    public void setUsr(String usr) { this.usr = usr; }
    public void setPasswd(String passwd) { this.passwd = passwd; }
   
    public static void main(String[] args) {
        BeansConstants CONST = BeansConstants.getInstance("mysql");
        System.out.println("driver = " + CONST.dbdriver + "/nurl = " + CONST.dburl);
    }
}

 

/*****************************
 * 处理数据库的连接和访问
 * @author javen
 * @version 0.98
 *****************************/

package net.asales.mysql;

import java.sql.*;
import java.io.*;

public class Mysql {
    private Connection conn = null;
    private Statement stmt = null;
    private PreparedStatement prepstmt = null;

    //这是一个全局类,里面放置数据库的参数,如数据库主机 访问用户名 密码等
    private BeansConstants CONST = BeansConstants.getInstance("mysql");

    /**
    * 构造数据库的连接和访问类
    */
    public Mysql() throws Exception {
        Class.forName(CONST.dbdriver);
        conn = DriverManager.getConnection(CONST.dburl);
        stmt = conn.createStatement();
    }
    public Mysql(String sql) throws Exception {
        Class.forName(CONST.dbdriver);
        conn = DriverManager.getConnection(CONST.dburl);
        this.prepareStatement(sql);
    }

    /**
    * PreparedStatement
    * @return sql 预设SQL语句
    */
    public void prepareStatement(String sql) throws SQLException {
        prepstmt = conn.prepareStatement(sql);
    }

    public Connection getConn() { return this.conn; }
    public Statement getStmt() { return this.stmt; }
    public PreparedStatement getPrepstmt() { return this.prepstmt; }
   
    /**
    * 执行SQL语句返回字段集
    * @param sql SQL语句
    * @return ResultSet 字段集
    */
    public ResultSet executeQuery(String sql) throws SQLException {
        if (stmt != null) {
            return stmt.executeQuery(sql);
        } else return null;
    }

    public ResultSet executeQuery() throws SQLException {
        if (prepstmt != null) {
            return prepstmt.executeQuery();
        } else return null;
    }
   
    /**
    * 执行SQL语句
    * @param sql SQL语句
    */
    public void executeUpdate(String sql) throws SQLException {
        if (stmt != null)
            stmt.executeUpdate(sql);
    }

    public void executeUpdate() throws SQLException {
        if (prepstmt != null)
            prepstmt.executeUpdate();
    }

    /**
    * 关闭连接
    */
    public void close() throws Exception {
        if (stmt != null) {
            stmt.close();
            stmt = null;
        }
        if (prepstmt != null) {
            prepstmt.close();
            prepstmt = null;
        }
        conn.close();
        conn = null;
    }
}

 

//连接案例

<%@ page import="java.sql.*" %>
<jsp:useBean id="SQL" scope="session" class="net.asales.mysql.Mysql" />
sql = "SELECT * FROM table";

ResultSet rs = SQL.executeQuery(sql);

sql = "INSERT INTO table (id, name) values('', '')";

SQL.executeUpdate(sql);

0 0

相关博文

我的热门文章

img
取 消
img