JDBCUtil工具类(一级):
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCUtil {
{
try {
Class.forName(DRI);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static final String DRI = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/jiejie";
public static final String USERNAME = "root";
public static final String PASSWORD = "root";
// 获取连接方法
public static Connection getCollection() throws Exception {
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return connection;
}
// 释放资源
public static void release(ResultSet rs, PreparedStatement ps, Connection conn) throws Exception {
if (rs != null) {
rs.close();
rs = null;
System.out.println("ResultSet已经关闭");
}
if (ps != null) {
ps.close();
ps = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
}
JDBCUtil工具类(升级版):
package com.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class JDBCUtil {
{
try {
// 加载 类
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static {
Properties properties = new Properties();
try {
// 加载properties中的数据
properties.load(new FileInputStream("hhh.properties"));
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String driver;
public static String url;
public static String username;
public static String password;
// 获取连接方法
public static Connection getCollection() throws Exception {
Connection connection = DriverManager.getConnection(url, username, password);
return connection;
}
// 释放资源
public static void release(ResultSet rs, PreparedStatement ps, Connection conn) throws Exception {
if (rs != null) {
rs.close();
rs = null;
System.out.println("ResultSet已经关闭");
}
if (ps != null) {
ps.close();
ps = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
}
hhh.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jiejie
username=root
password=root
测试类:
package com.test;
import java.sql.Connection;
import java.sql.ResultSet;
public class Test3 {
public static void main(String[] args) {
try {
Connection connection = JDBCUtil.getCollection();
ResultSet i = connection.prepareStatement("select * from u_p").executeQuery();
while (i.next()) {
int a = i.getInt("id");
String name = i.getString("username");
String pw = i.getString("pw");
System.out.println(a + "----" + name + "-----" + pw);
}
JDBCUtil.release(i, null, connection);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}