用jdbc.properties文件连接数据库
2013-04-02 20:22
344 查看
package jdbc.test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 读取jdbc.properties文件并连接数据库 <br>
* 关闭数据库连接 <br>
* 返回Connection的方法
*
* @author Administrator
*
*/
public class JDBCProperties {
private String classDriver;
private String url;
private String username;
private String password;
private Connection connection;
private Statement statement;
private ResultSet resultSet;
/**
* 创建一个空参构造函数加载配置文件 取得参数连接数据库
*
* @return Connection
* @throws IOException
* @throws ClassNotFoundException
*/
public JDBCProperties() throws IOException, ClassNotFoundException {
// 输入文件
//路径为当前Classpath的路径.
InputStream inputStream = JDBCProperties.class.getClass()
.getResourceAsStream("/jdbc.properties");
System.out.println(inputStream);
java.util.Properties properties = new java.util.Properties();
if (inputStream != null)
properties.load(inputStream);
// 根据Key取得配置文件中的值
classDriver = properties.getProperty("classDriver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
System.out.println(classDriver);
System.out.println(url);
System.out.println(username);
System.out.println(password);
// 加载类文件
Class.forName(classDriver);
}
/**
*
*
* @return
* @throws SQLException
*/
public java.sql.Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
/**
* 关闭数据库连接信息
*/
public void closeConnceiont() {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
}
测试创建一个表
package jdbc.test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestJdbc {
private static String sql = "create table test(id int)";
public static void main(String[] args) throws Exception {
JDBCProperties jdbcProperties = new JDBCProperties();
Connection connection = jdbcProperties.getConnection();
Statement statement = connection.createStatement();
statement.executeUpdate(sql);
ResultSet resultSet = statement.getResultSet();
if (resultSet != null) {
while (resultSet.next()) {
System.out.println(resultSet.getObject(1));
}
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 读取jdbc.properties文件并连接数据库 <br>
* 关闭数据库连接 <br>
* 返回Connection的方法
*
* @author Administrator
*
*/
public class JDBCProperties {
private String classDriver;
private String url;
private String username;
private String password;
private Connection connection;
private Statement statement;
private ResultSet resultSet;
/**
* 创建一个空参构造函数加载配置文件 取得参数连接数据库
*
* @return Connection
* @throws IOException
* @throws ClassNotFoundException
*/
public JDBCProperties() throws IOException, ClassNotFoundException {
// 输入文件
//路径为当前Classpath的路径.
InputStream inputStream = JDBCProperties.class.getClass()
.getResourceAsStream("/jdbc.properties");
System.out.println(inputStream);
java.util.Properties properties = new java.util.Properties();
if (inputStream != null)
properties.load(inputStream);
// 根据Key取得配置文件中的值
classDriver = properties.getProperty("classDriver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
System.out.println(classDriver);
System.out.println(url);
System.out.println(username);
System.out.println(password);
// 加载类文件
Class.forName(classDriver);
}
/**
*
*
* @return
* @throws SQLException
*/
public java.sql.Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
/**
* 关闭数据库连接信息
*/
public void closeConnceiont() {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
}
测试创建一个表
package jdbc.test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestJdbc {
private static String sql = "create table test(id int)";
public static void main(String[] args) throws Exception {
JDBCProperties jdbcProperties = new JDBCProperties();
Connection connection = jdbcProperties.getConnection();
Statement statement = connection.createStatement();
statement.executeUpdate(sql);
ResultSet resultSet = statement.getResultSet();
if (resultSet != null) {
while (resultSet.next()) {
System.out.println(resultSet.getObject(1));
}
}
}
}
相关文章推荐
- 复习课程jdbc:使用配置文件properties进行连接数据库,数据库存取图片,批处理,时间戳,事物回滚等等
- 读取配置jdbc.properties文件连接数据库
- jdbc中从properties文件中读取数据库连接字符串
- jdbc基础 (二) 通过properties配置文件连接数据库
- jdbc基础 (二) 通过properties配置文件连接数据库
- spring的xml配置文件中获取jdbc.properties乱码导致连接数据库失败
- jdbc基础 (二) 通过properties配置文件连接数据库
- jdbc基础 (二) 通过properties配置文件连接数据库
- 2.通过原始的Driver接口通过读取Properties属性文件获取JDBC数据库连接Connection
- jdbc的数据库连接信息在属性文件中的配置(db.properties)
- 2.通过原始的Driver接口通过读取Properties属性文件获取JDBC数据库连接Connection
- Java之JDBC 通过加载properties文件来的两种方式来连接数据库
- 编码问题 关于hibernate jdbc数据库连接在xml配置与在properties文件配置的差异
- 用jdbc.properties文件配置连接数据库+心得
- JDBC 连接数据库工具类(properties文件)
- jdbc.properties各种数据库连接配置
- spring与hibernate整合-使用properties文件分离数据库连接设置
- spring与hibernate整合-使用properties文件分离数据库连接设置
- Spring使用C3P0,从db.properties文件中读取信息,连接数据库
- java jdbc使用配置文件连接数据库: