大家用的時候只需要把配置文件裡的內容改掉就可以啦!
配置文件Database.Property內容
jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver
jdbc.url=jdbc:odbc:mydata
jdbc.username=system
jdbc.password=manager
JAVABEAN文件內容
package community.database;
import java.io.*;
import java.util.*;
import java.sql.*;
/** *//**
*
Title: Database Manager
*
Description: Connect Database
*
Copyright: Copyright 2002 YaoGuodong
*
Company:
* @author unascribed YaoGuodong 姚國棟
* @version 1.0
* @email [email protected]
* @QQ 16898283
* @轉載的時候請保留版權信息
*/
public class DBManager ...{
/** *//**getConnection() 得到連接數據庫的屬性文件內容,並返回連接結果集
@throws SQLException
@throws IOException
@throws Exception
聲明drivers,得到數據庫的驅動程序
聲明url,得到連接遠程數據庫的URL
聲明username,得到連接遠程數據庫的用戶名
聲明password,得到連接遠程數據庫的密碼
@return DriverManger.getConnection(url,username,passowrd);
*/
public Connection getConnection() throws SQLException,IOException,Exception ...{
Properties props = new Properties();
//String fileName = "e:\Database.Property";
//FileInputStream in = new FileInputStream(fileName);
InputStream in = getClass().getResourceAsStream("/Database.Property") ;
props.load(in);
String drivers = props.getProperty("jdbc.drivers");
if (drivers!=null)
...{ System.setProperty("jdbc.drives",drivers); }
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
Class.forName(drivers);
return DriverManager.getConnection(url,username,password);
}
/** *//**DBManager()
* 連接數據庫
*/
public DBManager() ...{
try...{
conn = getConnection();
stmt=conn.createStatement();
System.out.println("Connect Database is Ok!");
}
catch(Exception e)...{
System.out.println("Connect Database Failed!:"+e);
}
}
/** *//** executeQuery()方法,查詢SQL結果
@param sqlwords 為傳入的查詢的sql語句
@return rs 為返回的查詢結果集
*/
public ResultSet executeQuery(String sqlwords) ...{
this.sqlwords=sqlwords;
try...{
rs=stmt.executeQuery(sqlwords);
}
catch(SQLException ex)...{
System.out.println("Execute Query Sql Failed!:" + ex.getMessage());
}
return rs;
}
/** *//** executeUpdate()方法,修改數據庫記錄
@param sqlwords 為傳入的查詢的sql語
@return true|false
*/
public boolean executeUpdate(String sqlwords) ...{
this.sqlwords=sqlwords;
try ...{
stmt.executeUpdate(sqlwords);
return true;
}
catch(SQLException ex) ...{
System.err.println("Execute Update Sql Failed!: " + ex.getMessage());
return false;
}
}
/** *//** executeInsert()方法,插入新的數據庫記錄
@param sqlwords 為傳入的插入的sql語
@return true|false
*/
public boolean executeInsert(String sqlwords) ...{
this.sqlwords=sqlwords;
try ...{
stmt.executeUpdate(sqlwords);
return true;
}
catch(SQLException ex) ...{
System.err.println("Execute Insert Sql Failed!: " + ex.getMessage());
return false;
}
}
/** *//** executeDelete()方法,刪除數據庫記錄
@param sqlwords 為傳入的刪除的sql語
@return true|false
*/
public boolean executeDelete(String sqlwords) ...{
this.sqlwords=sqlwords;
try ...{
stmt.executeUpdate(sqlwords);
return true;
}
catch(SQLException ex) ...{
System.err.println("Execute Delete Sql Failed!: " + ex.getMessage());
return false;
}
}
/** *//** close()方法,斷開數據庫的連接
@return true|false
*/
public boolean close()
...{
try ...{
if(rs != null)...{ rs.close(); }
if(stmt != null)...{ stmt.close(); }
if(conn != null)...{ conn.close(); }
return true;
}
catch(Exception e) ...{
System.out.print("Clost Database Connect Failed!:"+e);
return false;
}
}
/** *//**
* 聲明conn
* 聲明rs結果集
* 聲明stmt語氣
* 聲明sqlwords關鍵字
*/
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
private String sqlwords = null;