如何连接mysql通过jdbc.properties
发布网友
发布时间:2022-04-07 17:42
我来回答
共2个回答
懂视网
时间:2022-04-07 22:04
JDBC连接数据库基本的步骤是固定的,这样就可以考虑封装一个工具类来简化数据库操作。
封装时用到了Java中的properties配置文件,是以一种键值对的形式存在的,可以把连接数据库要动态的信息保存到里面,这样比较直观,不容易出错,而且容易维护。
把配置文件放到src下就可以,如果要放到包下面就配置文件的相对路径就必须从包名开始。
Demo :
db.properties
mysqlDriver=com.mysql.jdbc.Driver
mysqlURL=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8
mysqlUser=root
mysqlPwd=1234
oracleDriver=oracle.jdbc.driver.OracleDriver
oracleURL=jdbc:oracle:thin:@localhost:1521:orcl
oracleUser=scott
oraclePwd=tiger
JDBCUtil.java
package com.wxisme.jdbcutil;
import java.io.Closeable;
import java.io.FileNotFoundException;
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;
import java.util.Properties;
/**
* JDBC数据库操作工具类 简化数据库操作
* @author wxisme
*
*/
public class JDBCUtil {
//资源文件
static Properties pros = null;
//静态初始化 当加载JDBCUtil类时调用
static {
pros = new Properties();
try {
//加载资源文件
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
if(in == null) {
throw new FileNotFoundException("配置文件未找到");
}
pros.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接
* @return Connection
*/
public static Connection getMySQLConnection() {
Connection conn = null;
try {
//加载数据库驱动
Class.forName(pros.getProperty("mysqlDriver"));
//获取数据库连接
conn = DriverManager.getConnection(pros.getProperty("mysqlURL"),
pros.getProperty("mysqlUser"),pros.getProperty("mysqlPwd"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Connection getOracleConn(){
try {
Class.forName(pros.getProperty("oracleDriver"));
return DriverManager.getConnection(pros.getProperty("oracleURL"),
pros.getProperty("oracleUser"),pros.getProperty("oraclePwd"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 关闭io资源
* @param io
*/
public static void closeFile(Closeable ... io) {
for(Closeable temp : io) {
if(temp != null) {
try {
temp.close();
} catch (IOException e) {
System.out.println("文件关闭失败");
e.printStackTrace();
}
}
}
}
//关闭JDBC资源 注意顺序
public static void close(ResultSet rs,Statement ps,Connection conn) {
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement ps,Connection conn){
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn){
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
UtilTest.java
package com.wxisme.jdbcutil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UtilTest {
public static void main(String[] args) {
Connection conn = JDBCUtil.getMySQLConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from student where id = ?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
rs = ps.executeQuery();
while(rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
还可以进一步封装成线程安全的工具类。
封装JDBC工具类
标签:
热心网友
时间:2022-04-07 19:12
如何连接mysql通过jdbc.properties
该例子将jdbc的链接信息封装到properties文件中。并将jdbc的基本使用框架提取到一个jdbcUtils类中。简化了jdbc操作数据库的操作。
JdbcUtil工具类:
[java] view plain copy
package com.xiaohui.jdbc;
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;
import java.util.Properties;
public final class JdbcUtil {
private static String url;//数据库链接字符串
private static String user;//用户名
private static String pass;//密码
private static String driver;//数据库驱动
//加载类时,先将jdbc链接数据库信息获取并赋值
static {
Properties prop = new Properties();
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("com/xiaohui/jdbc/info.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
url = prop.getProperty("url");
user = prop.getProperty("user");
pass = prop.getProperty("pass");
driver = prop.getProperty("driver");
}
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//取得链接
public static Connection getMySqlConnection(){
Connection conn=null;
try {
conn = DriverManager.getConnection(url,user,pass);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭结果集
public static void close(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭封装SQl命令的对象
public static void close(Statement state){
if(state!=null){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭链接
public static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
当很很多用户同时操作数据库时,获取到的都是一个conn链接对象。这样肯定不行。为了让每个线程都对应唯一的conn的链接对象可对上面的工具类做如下修改:
[java] view plain copy
package com.xiaohui.jdbc.util;
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;
import java.util.Properties;
public final class JdbcUtil {
private static String url;
private static String user;
private static String pass;
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
private static String driver;
static {
Properties prop = new Properties();
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream(
"com/xiaohui/jdbc/config/info.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
url = prop.getProperty("url");
user = prop.getProperty("user");
pass = prop.getProperty("pass");
driver = prop.getProperty("driver");
}
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 让每个线程都对应唯一的一个Connection 对象
public static Connection getMySqlConnection() {
Connection conn = tl.get();
if(conn==null){
//第一次启动该线程没有绑定conn
try {
conn = DriverManager.getConnection(url, user, pass);
//将conn绑定到tl中。
tl.set(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
public static void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement state) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void begin() {
Connection conn = getMySqlConnection();
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void commit() {
Connection conn = getMySqlConnection();
try {
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void rollback() {
Connection conn = getMySqlConnection();
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}