java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

java链接数据库的一个CLASS



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 getDataSource()
    {
        
return ds;
    }

    
public Connection getConnection()
    {
        
return conn;
    }

    
public void setAutoCommit(boolean bool)
        
throws SQLException
    {
        conn.setAutoCommit(bool);
    }

    
public void prepareStatement(String sql)
        
throws SQLException
    {
        clearParameters();
        prepstmt 
= conn.prepareStatement(sql, 10041008);
    }

    
public ResultSet executePrepQuery()
        
throws SQLException
    {
        
if (prepstmt != null)
            
return prepstmt.executeQuery();
        
else
            
return null;
    }

    
public int executePrepUpdate()
        
throws SQLException
    {
        
if (prepstmt != null)
            
return prepstmt.executeUpdate();
        
else
            
return 0;
    }

    
public void setString(int index, String value)
        
throws SQLException
    {
        prepstmt.setString(index, value);
    }

    
public void setInt(int index, int value)
        
throws SQLException
    {
        prepstmt.setInt(index, value);
    }

    
public void setBoolean(int index, boolean value)
        
throws SQLException
    {
        prepstmt.setBoolean(index, value);
    }

    
public void setDate(int index, Date value)
        
throws SQLException
    {
        prepstmt.setDate(index, value);
    }

    
public void setLong(int index, long value)
        
throws SQLException
    {
        prepstmt.setLong(index, value);
    }

    
public void setFloat(int index, float value)
        
throws SQLException
    {
        prepstmt.setFloat(index, value);
    }

    
public void setBytes(int index, byte value[])
        
throws SQLException
    {
        prepstmt.setBytes(index, value);
    }

    
public void setInteger(int index, Integer value)
        
throws SQLException
    {
        prepstmt.setLong(index, value.longValue());
    }

    
public void setShort(int index, short value)
        
throws SQLException
    {
        prepstmt.setShort(index, value);
    }

    
private void clearParameters()
    {
        
try
        {
            prepstmt.clearParameters();
            prepstmt.close();
            prepstmt 
= null;
        }
        
catch (Exception exception) { }
    }

    
private void clearStmt()
    {
        
try
        {
            stmt.close();
            stmt 
= null;
        }
        
catch (Exception exception) { }
    }

    
public int executeUpdate(String sql)
        
throws SQLException
    {
        clearStmt();
        
if (stmt == null)
            stmt 
= conn.createStatement();
        
return stmt.executeUpdate(sql);
    }

    
public ResultSet executeQuery(String sql)
        
throws SQLException
    {
        clearStmt();
        
if (stmt == null)
            stmt 
= conn.createStatement(10041008);
        
return stmt.executeQuery(sql);
    }

    
public void commit()
        
throws SQLException
    {
        conn.commit();
    }

    
public void rollback()
        
throws SQLException
    {
        conn.rollback();
    }

    
public void close()
    {
        
try
        {
            
if (stmt != null)
            {
                stmt.close();
                stmt 
= null;
            }
            
if (prepstmt != null)
            {
                prepstmt.clearParameters();
                prepstmt.close();
                prepstmt 
= null;
            }
            
if (conn != null)
                conn.close();
        }
        
catch (SQLException ex)
        {
            System.out.println(
"数据库连接关闭错误:" + ex.getMessage());
        }
    }

    
public static void main(String args[])
    {
        DBConnect DBConnect1 
= new DBConnect();
    }
}
站长原创:www.pmjava.com

posted on 2009-06-17 09:43 rrong_m 阅读(1342) 评论(0)  编辑  收藏

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜