[color=darkblue]关键字[/color]: jdbc 数据库 java j2ee
1.DBConfig.properties
Properties代码
#属性文件,存放JDBC联接数据库的参数
#sql2005驱动
#DataBaseDriver = com.microsoft.sqlserver.jdbc.SQLServerDriver
#DataBaseURL = jdbc:sqlserver://localhost:1433;SelectMode=cursor;DatabaseName=drp
#DataBaseUser = sa
#DataBaseUserPassword =
#mysql驱动
DataBaseDriver = com.mysql.jdbc.Driver
DataBaseURL = jdbc:mysql://localhost:3306/drp
DataBaseUser = root
DataBaseUserPassword = root
2.DBConnectionException.java
Java代码
package com.mp.struts.util;
/**
* 该异常类用来封装联接数据库时可能出现的异常
* @author 黄良宝
*
*/
@SuppressWarnings("serial")
public class DBConnectionException extends Exception{
public DBConnectionException(String message) {
super(message);
}
}
package com.mp.struts.util;
/**
* 该异常类用来封装联接数据库时可能出现的异常
* @author 黄良宝
*
*/
@SuppressWarnings("serial")
public class DBConnectionException extends Exception{
public DBConnectionException(String message) {
super(message);
}
}
3.DBConnection.java
Java代码
package com.mp.struts.util;
import java.io.InputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
* 实用工具类,获得通过JDBC联接数据库的联接对象.
* 联接数据库所需的参数放在资源属性文件DBConfig.properties中,
* 该文件位于类路径的根目录下
* @author 黄良宝
*
*/
public class DBConnection {
private static String CONFIG_FILENAME = "DBConfig.properties";
private static String driverName = "";
private static String url = "";
private static String user = "";
private static String password = "";
/**
* 返回数据库联接
*
* @return Connection
* @throws DBConnectionException
* @throws SQLException
* @throws SQLException
*/
public static Connection getConnection() throws DBConnectionException, SQLException {
Properties prop = new Properties();
// 读取配置文件信息
InputStream in = DBConnection.class.getResourceAsStream("/"
+ CONFIG_FILENAME);
Connection conn = null;
try {
//装载属性文件
prop.load(in);
//从属性文件中读取JDBC联接数据库的参数
driverName = prop.getProperty("DataBaseDriver");
user = prop.getProperty("DataBaseUser");// 用户,口令
password = prop.getProperty("DataBaseUserPassword");//口令
url = prop.getProperty("DataBaseURL");// url
// 装载驱动程序
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
} catch (IOException e) {
throw new DBConnectionException("装载数据库配置文件" + CONFIG_FILENAME
+ "时错误!");
} catch (ClassNotFoundException e) {
throw new DBConnectionException("装载驱动程序" + driverName + "时错误!");
}
return conn;
}
/**
*
* @param conn
* @throws SQLException
*/
public static void closeConn(Connection conn) throws SQLException {
if(conn != null) {
conn.close();
conn = null;
}
}
/**
*
* @param stmt
* @throws SQLException
*/
public static void closeStmt(Statement stmt) throws SQLException {
if(stmt != null) {
stmt.close();
stmt = null;
}
}
/**
*
* @param rs
* @throws SQLException
*/
public static void closeRs(ResultSet rs) throws SQLException {
if(rs != null) {
rs.close();
rs = null;
}
}
/**
* 测试主函数
* 如果未抛出异常则说明正确
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Connection conn = DBConnection.getConnection();
System.out.println("Connection -"+ conn.getCatalog() +" OK!");
DBConnection.closeConn(conn);
System.out.println("Connection is closed! now connection = " + conn.getCatalog());
}
}
1.DBConfig.properties
Properties代码
#属性文件,存放JDBC联接数据库的参数
#sql2005驱动
#DataBaseDriver = com.microsoft.sqlserver.jdbc.SQLServerDriver
#DataBaseURL = jdbc:sqlserver://localhost:1433;SelectMode=cursor;DatabaseName=drp
#DataBaseUser = sa
#DataBaseUserPassword =
#mysql驱动
DataBaseDriver = com.mysql.jdbc.Driver
DataBaseURL = jdbc:mysql://localhost:3306/drp
DataBaseUser = root
DataBaseUserPassword = root
2.DBConnectionException.java
Java代码
package com.mp.struts.util;
/**
* 该异常类用来封装联接数据库时可能出现的异常
* @author 黄良宝
*
*/
@SuppressWarnings("serial")
public class DBConnectionException extends Exception{
public DBConnectionException(String message) {
super(message);
}
}
package com.mp.struts.util;
/**
* 该异常类用来封装联接数据库时可能出现的异常
* @author 黄良宝
*
*/
@SuppressWarnings("serial")
public class DBConnectionException extends Exception{
public DBConnectionException(String message) {
super(message);
}
}
3.DBConnection.java
Java代码
package com.mp.struts.util;
import java.io.InputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
* 实用工具类,获得通过JDBC联接数据库的联接对象.
* 联接数据库所需的参数放在资源属性文件DBConfig.properties中,
* 该文件位于类路径的根目录下
* @author 黄良宝
*
*/
public class DBConnection {
private static String CONFIG_FILENAME = "DBConfig.properties";
private static String driverName = "";
private static String url = "";
private static String user = "";
private static String password = "";
/**
* 返回数据库联接
*
* @return Connection
* @throws DBConnectionException
* @throws SQLException
* @throws SQLException
*/
public static Connection getConnection() throws DBConnectionException, SQLException {
Properties prop = new Properties();
// 读取配置文件信息
InputStream in = DBConnection.class.getResourceAsStream("/"
+ CONFIG_FILENAME);
Connection conn = null;
try {
//装载属性文件
prop.load(in);
//从属性文件中读取JDBC联接数据库的参数
driverName = prop.getProperty("DataBaseDriver");
user = prop.getProperty("DataBaseUser");// 用户,口令
password = prop.getProperty("DataBaseUserPassword");//口令
url = prop.getProperty("DataBaseURL");// url
// 装载驱动程序
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
} catch (IOException e) {
throw new DBConnectionException("装载数据库配置文件" + CONFIG_FILENAME
+ "时错误!");
} catch (ClassNotFoundException e) {
throw new DBConnectionException("装载驱动程序" + driverName + "时错误!");
}
return conn;
}
/**
*
* @param conn
* @throws SQLException
*/
public static void closeConn(Connection conn) throws SQLException {
if(conn != null) {
conn.close();
conn = null;
}
}
/**
*
* @param stmt
* @throws SQLException
*/
public static void closeStmt(Statement stmt) throws SQLException {
if(stmt != null) {
stmt.close();
stmt = null;
}
}
/**
*
* @param rs
* @throws SQLException
*/
public static void closeRs(ResultSet rs) throws SQLException {
if(rs != null) {
rs.close();
rs = null;
}
}
/**
* 测试主函数
* 如果未抛出异常则说明正确
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Connection conn = DBConnection.getConnection();
System.out.println("Connection -"+ conn.getCatalog() +" OK!");
DBConnection.closeConn(conn);
System.out.println("Connection is closed! now connection = " + conn.getCatalog());
}
}