当前位置: 首页 > 工具软件 > DBConnection > 使用案例 >

JDBC工具类Dbconnection的细节

方轩昂
2023-12-01

错误提示:No operations allowed after connection closed。


这样的问题原因只有一个,你这里和数据库的连接Connection是一个Static的,程序共享这一个Connection。所以第一次对数据库操作没问题,当把Connection关闭后,第二次还想操作数据库时Connection肯定不存在了。



具体代码:

package com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbConnection {
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/struts2?useUnicode=true&characterEncoding=utf-8";
	private static String userName = "root";
	private static String userPassword = "root";

	public   static Connection getConnection() {
		Connection conn = null;//定义一个conn变量,不能定义为static的类型的   否则全局共享

		try {

			Class.forName(driver);
			try {
				conn = DriverManager.getConnection(url, userName, userPassword);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return conn;
	}

	public static void closeDB(Connection conn, PreparedStatement pstm,
			ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (pstm != null) {
			try {
				pstm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

调用工具类的DAO代码


package com.dao.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



import com.db.DbConnection;
import com.model.user.EndUser;

public class UserDao {
	// 向数据库插入一个用户的方法
	public int save(EndUser user) throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		conn = DbConnection .getConnection();
		int row = 0;
		String sql = "insert user(username,password,relname)values(?,?,?)";

		try {
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, user.getUsername());
			pstm.setString(2, user.getPassword());
			pstm.setString(3, user.getRelname());
			row = pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbConnection.closeDB(conn, pstm, rs);
		}
		return row;

	}

	public EndUser find(EndUser user) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		conn = DbConnection .getConnection();
		EndUser user2 = null;
		String sql = "select * from  user where  username=? and password=?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getUsername());
			pstmt.setString(2, user.getPassword());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				user2 = new EndUser();
				user2.setId(rs.getInt("id"));
				user2.setUsername(rs.getString("username"));
				user2.setPassword(rs.getString("password"));
				user2.setRelname(rs.getString("relname"));

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbConnection.closeDB(conn, pstmt, rs);

		}
		return user2;

	}

}


因为使用完数据库后,会有释放数据库资源的操作,如果设置为static则会共享一个conn连接对象,关闭后将不存在了





 类似资料: