📄 jdbcutil.java
字号:
package com.asnk120.EMH.persist;
import java.sql.*;
/**
* 定义的连接工具类,提供基本的连接及释放打印结果集的操作
*
* @author Administrator
*
*/
public class JdbcUtil {
static {
try {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
String url = "jdbc:mysql://localhost:3306/emh";
String username = "root";
String pwd = "c080724wei";
Connection con = null;
try {
con = DriverManager.getConnection(url, username, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void release(ResultSet rs, Statement stmt, Connection con) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (con != null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void release(Object o) {
try {
if (o instanceof ResultSet) {
((ResultSet) o).close();
} else if (o instanceof Statement) {
((Statement) o).close();
} else if (o instanceof Connection) {
((Connection) o).close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void printRs(ResultSet rs) {
if (rs == null) {
System.out.println("ResultSet is null");
return;
}
try {
ResultSetMetaData md = rs.getMetaData();
int cols = md.getColumnCount();
StringBuffer sb = new StringBuffer();
while (rs.next()) {
for (int i = 1; i <= cols; i++) {
sb.append(md.getColumnName(i) + "=");
sb.append(rs.getString(i) + " ");
}
sb.append("\n");
}
System.out.println(sb.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -