JDBCUtils.java
package jdbc;
import java.sql.*;
public class JDBCUtils {
/**
* 加载驱动 并建立数据库连接
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/demo?useSSL=false";
String username="root";
String password="root";
Connection connection=DriverManager.getConnection(url,username,password);
return connection;
}
/**
* 关闭数据库连接 释放资源
* @param statement
* @param connection
*/
public static void release(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
statement=null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection=null;
}
}
public static void release(Statement statement, Connection connection, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet=null;
}
release(statement, connection);
}
}
TestJdbc.java
package jdbc;
import java.sql.Connection;
import java.sql.SQLException;
public class TestJdbc {
public static void main(String[] args) {
try {
Connection connection=JDBCUtils.getConnection();
System.out.println(connection);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
运行效果