📄 conndb.java
字号:
package com.core;
import java.sql.*;
public class ConnDB {
public Connection conn=null;
public ResultSet rs=null;
public Statement stmt=null;
private String proxool = "com.mysql.jdbc.Driver";
private String poolname = "jdbc:mysql://localhost:3306/db_library";
private String username ="root";
private String password = "root";
//说明:此处需要配置web.xml文件
public ConnDB() { //构造方法
}
public Connection getConnection() {
try {
Class.forName(proxool);
conn = DriverManager.getConnection(poolname,username,password);
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (conn == null) {
System.out.println("没有获取到数据库连接");
}
return conn;
}
/*
*功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
/*
*功能:执行更新操作
*/
public int executeUpdate(String sql) {
int result = 0;
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql);
}
catch (SQLException ex) {
result = 0;
}
try {
stmt.close();
}
catch (SQLException ex1) {
}
return result;
}
/*
*功能:关闭数据库的连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (stmt != null) {
stmt.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (conn != null) {
conn.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -