java全俊方象垂議匯倖CLASS
扮寂:2011-10-13 blogjava rrong_m
package com.pmjava.jdbcdb;
import java.io.PrintStream;
import java.sql.*;
import java.util.Properties;
import javax.sql.DataSource;
public class DBConnect
{
private static DataSource ds;
private Connection conn;
private Statement stmt;
private PreparedStatement prepstmt;
private ResultSet rs;
private String dbDriver;
private String dbUrl;
private String dbUser;
private String dbPassword;
private DBConnect()
{
conn = null;
stmt = null;
prepstmt = null;
rs = null;
dbDriver = "";
dbUrl = "";
dbUser = "";
dbPassword = "";
try
{
init();
try
{
Class.forName(dbDriver);
}
catch (ClassNotFoundException e)
{
System.out.println("哈擬方象垂駁強危列?" + e.getMessage ());
}
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
conn.setAutoCommit(true);
stmt = conn.createStatement();
}
catch (SQLException e)
{
System.out.println("幹秀方象垂銭俊危列?" + e.getMessage());
}
}
private void init()
{
java.io.InputStream is = getClass().getResourceAsStream ("/database.properties");
Properties dbProps = new Properties();
try
{
dbProps.load(is);
dbDriver = dbProps.getProperty("driver", "com.mysql.jdbc.Driver");
dbUrl = dbProps.getProperty("url", "jdbc:mysql://localhost/db? autoReconnect=true&defaultAutoCommit=false&useUnicode=true&characterEncoding=gbk ");
dbUser = dbProps.getProperty("username", "1111111");
dbPassword = dbProps.getProperty("password", "111111");
}
catch (Exception e)
{
System.err.println("音嬬響函奉來猟周:萩鳩隠database.properties壓 CLASSPATH峺協議揃抄嶄");
return;
}
}
public static DBConnect getInstance()
{
return new DBConnect();
}
public DataSource g
|