/**
* 大家用的时候只需要把配置文件里的内容改掉就可以啦!
*
* 配置文件Database.Property内容
* jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver
* jdbc.url=jdbc:odbc:mydata
* jdbc.username=system
* jdbc.password=manager
*
*/packagepro03;// package community.database;importjava.io.*;importjava.util.*;importjava.sql.*;/**
* Title: Database Manager
* Description: Connect Database
* Copyright: Copyright 2002 YaoGuodong
* Company:
* @author unascribed YaoGuodong 姚国栋
* @version 1.0
* @email net_spirit@163.com
* @QQ 16898283
* @转载的时候请保留版权信息
*/publicclassDBManager {/**getConnection() 得到连接数据库的属性文件内容,并返回连接结果集
@throws SQLException
@throws IOException
@throws Exception
声明drivers,得到数据库的驱动程序
声明url,得到连接远程数据库的URL
声明username,得到连接远程数据库的用户名
声明password,得到连接远程数据库的密码
@return DriverManger.getConnection(url,username,passowrd);
*/publicConnection getConnection()throwsSQLException, IOException, Exception {
Properties props =newProperties();//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);returnDriverManager.getConnection(url, username, password);
}/**DBManager()
* 连接数据库
*/publicDBManager() {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 为返回的查询结果集
*/publicResultSet executeQuery(String sqlwords) {this.sqlwords = sqlwords;try{
rs = stmt.executeQuery(sqlwords);
}catch(SQLException ex) {
System.out.println("Execute Query Sql Failed!:"+ ex.getMessage());
}returnrs;
}/** executeUpdate()方法,修改数据库记录
@param sqlwords 为传入的查询的sql语
@return true|false
*/publicbooleanexecuteUpdate(String sqlwords) {this.sqlwords = sqlwords;try{
stmt.executeUpdate(sqlwords);returntrue;
}catch(SQLException ex) {
System.err.println("Execute Update Sql Failed!: "+ ex.getMessage());returnfalse;
}
}/** executeInsert()方法,插入新的数据库记录
@param sqlwords 为传入的插入的sql语
@return true|false
*/publicbooleanexecuteInsert(String sqlwords) {this.sqlwords = sqlwords;try{
stmt.executeUpdate(sqlwords);returntrue;
}catch(SQLException ex) {
System.err.println("Execute Insert Sql Failed!: "+ ex.getMessage());returnfalse;
}
}/** executeDelete()方法,删除数据库记录
@param sqlwords 为传入的删除的sql语
@return true|false
*/publicbooleanexecuteDelete(String sqlwords) {this.sqlwords = sqlwords;try{
stmt.executeUpdate(sqlwords);returntrue;
}catch(SQLException ex) {
System.err.println("Execute Delete Sql Failed!: "+ ex.getMessage());returnfalse;
}
}/** close()方法,断开数据库的连接
*@return true|false
*/publicbooleanclose() {try{if(rs !=null) {
rs.close();
}if(stmt !=null) {
stmt.close();
}if(conn !=null) {
conn.close();
}returntrue;
}catch(Exception e) {
System.out.print("Clost Database Connect Failed!:"+ e);returnfalse;
}
}/**
* 声明conn
* 声明rs结果集
* 声明stmt语气
* 声明sqlwords关键字
*/Connection conn =null;
ResultSet rs =null;
Statement stmt =null;privateString sqlwords =null;

本文介绍了一个Java应用程序如何通过配置文件连接数据库并执行基本的CRUD操作。提供了详细的代码实现,包括获取连接、执行查询、更新、插入和删除操作的方法。
1374

被折叠的 条评论
为什么被折叠?



