JAVA 数据库连接

package xx72.Data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 管理数据库连接信息
 * 
 * @author lj
 * 
 */
public class DBInfo {
    static {
        try {
            Class.forName("com.oscar.Driver");
        } catch (java.lang.ClassNotFoundException e) {
            System.err.println(e.getMessage());
        }
    }
    private String URL = "jdbc:oscar://10.0.5.252:2004/KSTORE";
    private String USERNAME = "";
    private String PASSWORD = "";
    private Connection con;

    /**
     * 默认构造方法
     */
    public DBInfo() {
        try {
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 构造方法
     * 
     * @param url
     *            数据库url
     * @param username
     *            用户名
     * @param password
     *            密码
     */
    public DBInfo(String url, String username, String password) {
        this.URL = url;
        this.USERNAME = username;
        this.PASSWORD = password;
        try {
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接
     * 
     * @return
     */
    public Connection getCon() {
        return con;
    }

}

 第一步:加载数据库驱动 Class.forName("com.oscar.Driver");

第二步:JAVA数据库连接 con = DriverManager.getConnection(URL, USERNAME, PASSWORD);

第三步:创建PreparedStatement pstmt = (PreparedStatement) con.createStatement();

第四步:执行sql语句 rs = pstmt.executeQuery(ctsql);

posted on 2013-08-15 09:33  ywf—java  阅读(400)  评论(0编辑  收藏  举报

导航