JDBCUtils——原生
时间:2021-07-01 10:21:17
帮助过:11人阅读
java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
导包:
mysql-connector-java-5.1.37-bin.jar
*/
public class JdbcUtils {
private static String DRIVER = "com.mysql.jdbc.Driver"
;
private static String URL = "jdbc:mysql://localhost:3306/newdatabase"
;
private static String USER = "root"
;
private static String PASSWORD = "1234"
;
static {
try {
Class.forName(DRIVER);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 获得连接
*
* @return
* @throws SQLException
*/
public static Connection getConnection()
throws SQLException {
Connection connection =
DriverManager.getConnection(URL, USER, PASSWORD);
return connection;
}
/**
* 释放资源
*
* @param connection
* @param statement
* @param resultSet
*/
public static void closeResouce(Connection connection, Statement statement, ResultSet resultSet) {
if (resultSet !=
null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement !=
null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection !=
null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
JDBCUtils——原生
标签:cut time tac str stack get runtime turn jdbcutil