package dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import entity.Film;
public class DBUtil {
private Connection con = null;
private PreparedStatement pst = null;
private ResultSet rs = null;
/**
* 连接数据库的方法
* @return 数据库连接
*/
public Connection getConnection(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
return DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=film", "sa", "sa");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("加载驱动失败");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("连接数据库失败");
}
return null;
}
/**
* 关闭数据库连接 , 释放资源
*/
public void close(){
try {
if (rs != null){
rs.close();
}
if (pst != null){
pst.close();
}
if (con != null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 更新所座位号状态的方法
* @return
*/
public boolean update(){
boolean flag = false;
try {
con = getConnection();
pst = con.prepareStatement("update film set state=0");
int row = pst.executeUpdate();
flag = row > 0 ? true : false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return flag;
}
/**
* 根据座位号更新座位状态
* @param seatId 座位号
* @return
*/
public boolean update(String seatId){
boolean flag = false;
try {
con = getConnection();
pst = con.prepareStatement("update film set state=1 where seatId=?");
pst.setString(1, seatId);
int row = pst.executeUpdate();
flag = row > 0 ? true : false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return flag;
}
public HashMap<String, List<Film>> getAll(){
HashMap<String, List<Film>> map = new HashMap<String, List<Film>>();
try {
con = getConnection();
pst = con.prepareStatement("select * from film order by seatId");
rs = pst.executeQuery();
List<Film> list;
String key = "null";
Film f;
list = new ArrayList<Film>();
while (rs.next()) {
f = new Film();
f.setSeatId(rs.getString(1));
if (!f.getSeatId().substring(0,1).equals(key)){
if (list.size() > 0){
map.put(key, list);
}
list = new ArrayList<Film>();
key = f.getSeatId().substring(0,1);
}
f.setState(rs.getInt(2));
list.add(f);
}
map.put(key, list);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
close();
}
return map;
}
}
用JSP开发的影院售票系统
4星 · 超过85%的资源 需积分: 9 134 浏览量
2009-04-24
10:32:50
上传
评论 3
收藏 364KB RAR 举报
姓孙名悟空
- 粉丝: 11
- 资源: 8
最新资源
- java-leetcode面试题解Stack之第284题窥视迭代器-题解.zip
- go语言,window系统下安装go语言包1.18.3和1.20.4
- 基于LDA模型的医疗信息化政策主题提取与热点分析.pdf
- VisualBasic形考任务一实验1 欢迎程序《源代码》
- Scratch我的世界《源码》
- Delphi 12含工具、安装包、控件源代码
- JAVA Springboot汽车租赁系统
- IM系统的音乐聊天室(基于小程序+Socket+Node实现)
- 微信小程序商城系统(电商系统)(SpringBoot+Vue3)(至尊版) java毕业设计 源码+sql脚本+论文 完整版
- Pandas 透视表和交叉表
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈