📄 dbconn.java
字号:
package com.puckasoft.video312.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBConn {
private static String jdbcDriver;
private static String jdbcurl;
private static String userName;
private static String password;
public static Connection getConnection() {
Connection conn = null;
try {
if (jdbcDriver == null) {
InputStream is = DBConn.class.getClassLoader()
.getResourceAsStream("mysql.properties");
Properties prop = new Properties();
try {
prop.load(is);
jdbcDriver = prop.getProperty("mysql.driver");
jdbcurl = prop.getProperty("mysql.url");
userName = prop.getProperty("mysql.userName");
password = prop.getProperty("mysql.password");
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Class.forName(jdbcDriver);
conn = DriverManager.getConnection(jdbcurl, userName, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(Statement st, Connection conn) {
try {
if (st != null)
st.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Connection conn = DBConn.getConnection();
System.out.println(conn);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -