您的位置:首页 > 编程语言 > Java开发

javaweb_day7(JDBC)的配置信息提取到配置文件

2017-10-25 21:25 267 查看

JDBC配置信息提取到配置文件

1.首先需要在工程下创建一个jdbc.properties文件

【jdbc.properties内容】

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///jdbc_demo
username=root
password=root




2.读取文件里面的信息

通过:Properties properties =new Properties();对象里面的load方法



3.配置文件的工具类

package com.jdbc.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Jdbcutils {

private static final String driverClassName;//驱动
private static final String url;//连接数据库的地址
private static final String username;//用户名
private static final String password;//密码

static{
//创建对象
Properties properties =new Properties();

try {

//加载配置文件
properties.load(new FileInputStream("src/jdbc.properties"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//读取到的配置信息在赋值
driverClassName=properties.getProperty("driverClassName");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");

}
/**
* 注册驱动的方法
*/
public static void loadDriver(){
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
* 获得连接的方法
*/
public static Connection getConnection(){
Connection conn = null;
try{
// 将驱动一并注册:
loadDriver();
// 获得连接
conn = DriverManager.getConnection(url,username, password);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}

/**
* 释放资源的方法
*/
public static void release(Statement stmt,Connection conn){
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}

stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}

public static void release(ResultSet rs,Statement stmt,Connection conn){
// 资源释放:
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}

rs = null;
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}

stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}


【测试工具类】

案例:查询

package com.jdbc.demo2;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.Test;

import com.jdbc.utils.Jdbcutils;

public class Demo2 {

// 查询
@Test
public void testSeach() throws Exception {

Connection connection = null;// 连接对象
Statement statement = null;// 执行sql语句对象
ResultSet resultSet = null;// 返回结果集对象

connection = Jdbcutils.getConnection();// 获取连接

statement = connection.createStatement();// 创建执行sql语句对象
String sql = "select * from t_user";//编写sql语句
resultSet = statement.executeQuery(sql);//执行sql语句

//遍历结果集
while (resultSet.next()) {

System.out.println(resultSet.getInt("id") + "  " + resultSet.getString("name"));

}

//释放资源
Jdbcutils.release(resultSet, statement, connection);

}

}


测试成功:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐