📄 databaseoperate.java~11~
字号:
package com.cinema.db;
import java.sql.*;
import java.util.*;
import com.cinema.bean.*;
public class DatabaseOperate
{
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs";
Connection con= null;
public DatabaseOperate()
{
try
{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(Exception ce)
{
ce.printStackTrace();
}
}
public Connection getCon()
{
try
{
con = DriverManager.getConnection(url, "sa", "");
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return con;
}
public void closecon(ResultSet res,PreparedStatement pstmt,Statement stmt,Connection con)
{
try
{
if(res != null)
{
res.close();
}
if(stmt !=null)
{
stmt.close();
}
if(pstmt != null)
{
pstmt.close();
}
if(con != null)
{
con.close();
}
}
catch(Exception ce)
{
ce.printStackTrace();
}
}
public boolean updateTicket(String sql)
{
boolean flag = false;
con = this.getCon();
PreparedStatement pstmt = null;
try
{
pstmt = con.prepareStatement(sql);
int num = pstmt.executeUpdate();
flag = num>0?true:false;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
finally
{
try
{
this.closecon(null,pstmt,null,con);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
return flag;
}
public Vector queryTicket()
{
Vector vec = new Vector();
con = this.getCon();
PreparedStatement pstmt = null;
ResultSet res = null;
try
{
String sql = "select * from tab_ticket";
pstmt = con.prepareStatement(sql);
res = pstmt.executeQuery();
while(res.next())
{
TicketBean tb = new TicketBean();
tb.setSeat(res.getString(1));
tb.setTsign(res.getInt(2));
vec.add(tb);
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
finally
{
try
{
this.closecon(res, pstmt, null, con);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return vec;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -