package com.sfy.utils;java
import java.io.IOException;
import java.sql.*;
import java.util.Properties;mysql
public class JDBCUtils {sql
public static void main(String[] args) {url
getMysql_conn();
}get
static Properties properties;io
static {
properties = new Properties();class
try {
//properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
properties.load(JDBCUtils.class.getResourceAsStream("/db.properties"));
} catch (IOException e) {
e.printStackTrace();
System.out.printf("文件加載error...");
}import
}jdbc
public static Connection getMysql_conn(){im
Connection connection = null;
try {
Class.forName(properties.getProperty("driverName"));
connection = DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("user"),properties.getProperty("password"));
System.out.printf("sucess...");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.printf("error...");
}catch (SQLException e){
e.printStackTrace();
System.out.printf("error...");
}
return connection;
}
public static void close_conn(ResultSet resultSet, PreparedStatement preparedStatement,Connection connection){
//1 ResultSet 2.preparedStatement 3.connection
try {
if(resultSet!=null){
resultSet.close();
}
if (preparedStatement !=null){
preparedStatement.close();
}
if (connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close_conn( PreparedStatement preparedStatement,Connection connection){
//1 ResultSet 2.preparedStatement 3.connection
try {
if (preparedStatement !=null){
preparedStatement.close();
}
if (connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close_conn(Connection connection){
//1 ResultSet 2.preparedStatement 3.connection
try {
if (connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// db.propertes driverName =com.mysql.jdbc.Driver url =jdbc:mysql://localhost:3306/ekode user=root password=mysql