public class Buy extends JPanel implements ActionListener
{
Connection conn;
Statement stmt;
ResultSet rs;
String DBDRIVER ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=高铁动车售票系统";
String userName = "sa";
String userPwd = "123456";
private JSplitPane jsp1 =new JSplitPane(JSplitPane.VERTICAL_SPLIT,true);
private JPanel jp2 =new JPanel();
private JButton jb2 =new JButton("确定");
private JLabel jl3 =new JLabel("请输入要买的车票编号:");
private JTextField jtxt3 =new JTextField();
public void connDB() { //连接数据库方法
try {
conn=DriverManager.getConnection(dbURL,userName,userPwd);
stmt=conn.createStatement();
}catch(Exception e) {
e.printStackTrace();
System.out.println("连接失败!");
}
}
public void closeDB() { //关闭数据库方法
try {
rs.close();
stmt.close();
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
public boolean searchticketno(String str) {
boolean x=false;
this.connDB();
try {
rs=stmt.executeQuery("select * from 车票信息表");
while(rs.next()) {
if(rs.getString("车票编号").trim().equals(str)) { //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!
x=true;
}
}
}catch(Exception e) {
e.printStackTrace();
}
return x;
}
Vector head =new Vector();
{
head.add("车票编号");
head.add("车次");
head.add("座位等级");
head.add("票价");
head.add("座位号");
head.add("出发站");
head.add("目的站");
head.add("出发时间");
head.add("到站时间");
head.add("余票数量");
}
DataBase db= new DataBase();
Vector row =db.getdata();
DefaultTableModel dtm =new DefaultTableModel(row,head);
JTable jt =new JTable(dtm);
JScrollPane jspn =new JScrollPane(jt);
public Buy()
{
this.setLayout(new GridLayout(1,1));
jsp1.setTopComponent(jp2);
jsp1.setBottomComponent(jspn);
jsp1.setDividerLocation(80);
jsp1.setDividerSize(4);
jp2.setLayout(null);
jb2.setBounds(400,30,60,20);
jp2.add(jb2);
jb2.addActionListener(this);
jl3.setBounds(60, 30, 160, 20);
jp2.add(jl3);
jtxt3.setBounds(200, 30, 100, 20);
jp2.add(jtxt3);
this.add(jsp1);
this.setBounds(10, 10, 800, 600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb2)
{
//判定文本框内容,车票信息改变,余票数量-1,乘客购票成功
if(!searchticketno(jtxt3.getText().trim()))
{
JOptionPane.showMessageDialog(null,"对不起,车票编号不存在!请重新输入!");
jtxt3.setText("");
}
else
{
//未实现的功能
}
}
}
}
public class DataBase
{
private final static String URL = "jdbc:sqlserver://localhost:1433; DatabaseName=高铁动车售票系统";
private static final String USER="sa";
private static final String PASSWORD="123456";
private Vector<Object> row =new Vector() ;
private Vector rowsdata =new Vector();
private static Connection conn=null;
//静态代码块(将加载驱动、连接数据库放入静态块中)
static{
try {
//1.加载驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.获得数据库的连接
conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}
}
//对外提供一个方法来获取数据库连接
public static Connection getConnection(){
return conn;
}
//测试用例
// public static void main(String[] args) throws Exception
// {
//
// //3.通过数据库的连接操作数据库,实现增删改查
// Statement stmt = conn.createStatement();
// //ResultSet executeQuery(String sqlString):执行查询数据库的SQL语句 ,返回一个结果集(ResultSet)对象。
// ResultSet rs = stmt.executeQuery("select 车票编号,车次,座位等第,票价,座位号,出发站,目的站,出发时间,到站时间,余票数量 from 车票信息表");
// while(rs.next()){//如果对象中有数据,就会循环打印出来
// System.out.println(rs.getString("车票编号")+","+rs.getString("车次")+","+rs.getString("座位等第")+","+rs.getString("票价")+","+rs.getString("座位号")+","+rs.getString("出发站")+","+rs.getString("目的站")+","+rs.getString("出发时间")+","+rs.getString("到站时间")+","+rs.getString("余票数量"));
// }
// }
public Vector getdata()
{
try{
//3.通过数据库的连接操作数据库,实现增删改查
Statement stmt = conn.createStatement();
//ResultSet executeQuery(String sqlString):执行查询数据库的SQL语句 ,返回一个结果集(ResultSet)对象。
ResultSet rs = stmt.executeQuery("select * from 车票信息表");
while(rs.next()){//如果对象中有数据,就会循环打印出来
row.add(rs.getString("车票编号"));
row.add(rs.getString("车次"));
row.add(rs.getString("座位等第"));
row.add(rs.getString("票价"));
row.add(rs.getString("座位号"));
row.add(rs.getString("出发站"));
row.add(rs.getString("目的站"));
row.add(rs.getString("出发时间"));
row.add(rs.getString("到站时间"));
row.add(rs.getString("余票数量"));
rowsdata.add(row);
row=new Vector();
}}catch(SQLException e)
{
e.printStackTrace();
return null;
}
return rowsdata;
}
}
希望最后的结果就是在文本框内输入车票编号,然后检索数据库,有对应车票编码的车票就显示购票成功,同时刷新当前显示的数据表,更新余票数量。