JDBC util 工具类
程序员文章站
2022-05-19 12:05:34
...
package cj;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class JdbcUtil {
private static String url = "jdbc:oracle:thin:@192.168.1.102:1521:orcl";
private static String password = "cj";
private static String user = "cj";
private JdbcUtil() {
}
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, "cj", "cj");
}
public static void free(ResultSet rs, Statement st, Connection con) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (con != null)
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
上一篇: 怀柔智慧谷
下一篇: 【JDBC】JDBC工具类封装