模拟火车票管理系统
package com.qdlg.trainticketmanger.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import com.qdlg.trainticketmanger.model.DBConnection;
import com.qdlg.trainticketmanger.model.Ticket;
public class TicketManager {
/**
*
* @param train
* @return
*/
public int addTicket(Ticket ticket){
String sql="insert into ticket(ticketNumber,trainNumber,trainDate,startStation,endStation," +
"price,carriageNum,seatNum,type,grade,userId,trainTime) values(?,?,?,?,?,?,?,?,?,?,?,?)";
Connection conn=DBConnection.getConn();
PreparedStatement pst=null;
int number =0;
try {
pst = conn.prepareStatement(sql);
pst.setString(1,ticket.getTicketNumber());
pst.setString(2,ticket.getTrainNumber());
pst.setString(3,ticket.getTrainDate());
pst.setString(4,ticket.getStartStation());
pst.setString(5,ticket.getEndStation());
pst.setDouble(6,ticket.getPrice());
pst.setInt(7,ticket.getCarriageNum());
pst.setInt(8,ticket.getSeatNum());
pst.setString(9,ticket.getType());
pst.setString(10,ticket.getGrade());
pst.setInt(11,ticket.getUserId());
pst.setString(12,ticket.getTrainTime());
number=pst.executeUpdate();
//JOptionPane.showMessageDialog(null, "存储成功!", "SUCCESS", JOptionPane.INFORMATION_MESSAGE) ;
return number;
} catch (SQLException e) {
e.printStackTrace();
}
return number;
}
/**
* 批量添加
* @param list
* @return
*/
public int addAll(List list){
//建立数据库操作
Connection conn=DBConnection.getConn();
Statement sta=null;
try {
sta=conn.createStatement();
//将自动提交模式转换为手动提交模式
conn.setAutoCommit(false);
Iterator iter = list.iterator();
//循环将数据准换成SQL语句
while(iter.hasNext()){
Ticket ticket =iter.next();
sta.add
1