When you use it, you just have to change the contents of the configuration file.
Configuration file Database.property Content
jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver
jdbc.url=jdbc:odbc:mydata
jdbc.username=system
jdbc.password=manager
JavaBean File Contents
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 Yiu Dong
* @version 1.0
* @email net_spirit@163.com
* @QQ 16898283
* Please keep the copyright information when you reprint it
*/
public class Dbmanager ... {
/** *//**getconnection () Gets the contents of the properties file of the connected database and returns the connection result set
@throws SQLException
@throws IOException
@throws Exception
declare drivers, get the driver for the database
the URL to connect to the remote database
declare username, get the username to connect to the remote database
declares password, gets the password to connect to the remote database
@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 ()
* Connection Database
*/
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 () method, query SQL results
@param sqlwords The SQL statement for the incoming query
@return RS for the returned query result set
*/
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 () method, modify database records
@param sqlwords The SQL for the incoming query
@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 () method, insert a new database record
@param sqlwords SQL for incoming inserts
@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 () method, deleting database records
@param sqlwords for incoming deletion of 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 () method to disconnect the database
@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;
}
}
/** *//**
* Declaration Conn
* Declaration RS result set
* Statement stmt Tone
* Declaration sqlwords Keyword
*/
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
private String sqlwords = null;