我是个新手,在session会话开始的时候连接的数据库,但是在什么时候关闭数据库的连接呢。我想在session会话结束时关闭连接,应该怎么写?
(自问自答)不好意思,麻烦大家了。
我做的是一个银行系统小demo(web project),本来是想一个session会话就连接一次数据库,当这个session会话结束时进行数据库的断开,可以省点资源。
但是没有找到合适的方法在合适的时间断开数据库。后来意识到这样做也会占用连接数据库的资源,没必要这么做。每次需要使用就连接,用完就关闭更合适。
后来知道这两种思路都不是很好的方法,可以使用数据库连接池,发现一个网址https://blog.csdn.net/dazern/article/details/385130
session.close()
package com.database.dao.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.database.dao.User;
import com.database.dao.vo.UserInfo;
import com.database.factory.DAOFactory;
public class UserImpl implements User {
@Override
public void query() {
Connection conn = null;
Statement st=null;
ResultSet rs=null;
try {
conn = DAOFactory.getDriverMananger();
st = conn.createStatement();
rs = st.executeQuery("select * from user");
while(rs.next()){
UserInfo userInfo = new UserInfo();
/*int id=rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
int age = rs.getInt("age");
boolean sex = rs.getBoolean("sex");*/
//从1开始
userInfo.setId(rs.getInt(1));
userInfo.setUserName(rs.getString(2));
userInfo.setPassword(rs.getString(3));
userInfo.setAge(rs.getInt(4));
userInfo.setSex(rs.getBoolean(5));
System.out.println(userInfo.getId()+" "+userInfo.getUserName() +" "+userInfo.getPassword()+" "+userInfo.getAge()+" "+userInfo.getSex());
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
close(rs,st,conn);
}
}
@Override
public void insert(UserInfo userInfo) {
Connection conn = null;
Statement st=null;
ResultSet rs=null;
try {
String sql="INSERT INTO user(id,username,password,age,sex) VALUES(null,'"+userInfo.getUserName()+"','"+userInfo.getPassword()+"','"+userInfo.getAge()+"',"+userInfo.getSex()+");";
conn = DAOFactory.getDriverMananger();
st = conn.createStatement();
int num=st.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally{
close(rs,st,conn);
}
}
@Override
public void update() {
Connection conn = null;
Statement st=null;
ResultSet rs=null;
try {
conn = DAOFactory.getDriverMananger();
st = conn.createStatement();
st.addBatch("UPDATE user SET sex = false WHERE id=1");
st.addBatch("UPDATE user SET sex = false WHERE id=2");
st.addBatch("UPDATE user SET sex = false WHERE id=3");
int num[]=st.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}finally{
close(rs,st,conn);
}
}
@Override
public void delete() {
// TODO Auto-generated method stub
}
private void close(ResultSet rs,Statement st,Connection conn){
try {
if(rs!= null)
rs.close();
if(rs!= null)
st.close();
if(rs!= null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
rs.close();conn.close();
将connection存在session中,session结束之前先关闭数据库连接connection.close()
通过close函数关闭,或者调用dispose方法关闭
public class DBUtil {
//连接属性
private static String driver="";
private static String url="";
private static String user="";
private static String pwd="";
static{
try {
Properties properties = new Properties();
properties.load(DBUtil.class.getClassLoader().getResourceAsStream("DBUtil.properties"));
String type=properties.getProperty("type");
driver = properties.getProperty(type+"Driver");
url = properties.getProperty(type+"Url");
user = properties.getProperty(type+"User");
pwd = properties.getProperty(type+"Pwd");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接
* @return
*/
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(url,user,pwd);
} catch (SQLException e) {
System.out.println("DBUtil.getConnection(获取连接)["+url+"]["+user+"]["+pwd+"]");
e.printStackTrace();
}
return connection;
}
/**
* 获取Statement
* @param connection
* @return
*/
public static Statement getStatement(Connection connection) {
Statement statement = null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return statement;
}
/**
* 获取PreparedStatement
* @param connection
* @param sql
* @return
*/
public static PreparedStatement getPreparedStatement(Connection connection,String sql) {
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return preparedStatement;
}
/**
* 关闭资源
* @param connection
* @param statement
* @param resultSet
*/
public static void closeAll(Connection connection,Statement statement,ResultSet resultSet) {
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if(statement!=null){
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
拜托各位,我是在JSP页面里面,当session会话结束时候去关闭,我有一个javabean里,有自己的关闭函数destroy。就是不知道在JSP里面什么时候调用,该怎样写?
一般业务会放到jsp中关闭数据库连接吗?页面调用servlet,开关连接都不用页面管吧!
不好意思,麻烦大家了。
我做的是一个银行系统小demo(web project),本来是想一个session会话就连接一次数据库,当这个session会话结束时进行数据库的断开,以为可以省点资源。
但是没有找到合适的方法在合适的时间断开数据库。后来意识到这样做会占用连接数据库的资源,没必要这么做。每次需要使用就连接,用完就关闭更合适。
这两种思路都不是很好的方法,可以使用数据库连接池,发现一个网址https://blog.csdn.net/dazern/article/details/385130