jdbc封装工具类
public class DBUntil {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/qm?useUnicode=true&characterEncoding=utf-8";
private static final String USER = "root";
private static final String PASSWORD = "123";
private static Connection conn = null;
public static void loadDriver() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("加载失败");
}
}
public static Connection getConnection() {
try {
loadDriver();
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
System.out.println("连接出现问题");
}
return conn;
}
public static void release(Statement statement, Connection conn) {
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
e.getStackTrace();
}
statement = null;
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.getStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs, Statement statement, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.getStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
e.getStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.getStackTrace();
}
}
}
}
一些小知识
控制台监听回车
while (true) {
System.out.println("按下回车返回主菜单");
Scanner getEnter = new Scanner(System.in);
String isEnter = getEnter.nextLine();
if (isEnter.equals("")){
break;
}
}
jdbc模糊查询时where是变量
mailList.setName("%" + name + "%");
String sql = "SELECT * FROM maillist where name like " + "'" + mailList.getName() + "'";