经过了几天的实训做出了比较粗鄙的实训项目,也是供大家参考,互相学习,也希望能够能指出代码不足之处
接下来话不多说直接代码走起,如下:
FlightDaoImpl类
对数据库的增删改查的功能方法
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import po.Flight;
import util.DBConnection;
public class FlightDaoImpl implements IFlightDao{
public boolean findUser(String name,String passWord) {
try {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
conn = DBConnection.getConnection();//获取与数据库连接对象
String sql = "select * from user_info where user_name=? and user_pwd=?";//表示占位符
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, passWord);
rs = pstmt.executeQuery();
if (rs.next()) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Override
public List<Flight> findAll() {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from Flight";
ArrayList<Flight> list = new ArrayList<Flight>();
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){//查询得到的是多条数据,所以用while
Flight flight = new Flight();//如果查找到,创建对象
flight.setPnm(rs.getString(1));
flight.setPnumber(rs.getString(2));
flight.setDestin(rs.getString(3));
flight.setStime(rs.getString(4));
list.add(flight);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
DBConnection.closeAll(conn, pstmt, rs);
}
return list;
}
@Override
public List<Flight> findByKey(String date) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from Flight where stime=?";
ArrayList<Flight> list = new ArrayList<Flight>();
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, date);
rs = pstmt.executeQuery();
while(rs.next()){//查询得到的是多条数据,所以用while
Flight flight = new Flight();//如果查找到,创建对象
flight.setPnm(rs.getString(1));
flight.setPnumber(rs.getString(2));
flight.setDestin(rs.getString(3));
flight.setStime(rs.getString(4));
list.add(flight);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
DBConnection.closeAll(conn, pstmt, rs);
}
return list;
}
public List<Flight> findByKeyl(String aq) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from Flight where destin=?";
ArrayList<Flight> list = new ArrayList<Flight>();
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, aq);
rs = pstmt.executeQuery();
while(rs.next()){//查询得到的是多条数据,所以用while
Flight flight = new Flight();//如果查找到,创建对象
flight.setPnm(rs.getString(1));
flight.setPnumber(rs.getString(2));
flight.setDestin(rs.getString(3));
flight.setStime(rs.getString(4));
list.add(flight);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
DBConnection.closeAll(conn, pstmt, rs);
}
return list;
}
@Override
public int delete(String id) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "delete from Flight where Pnm = ?";
int result = 0;
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally{
DBConnection.closeAll(conn, pstmt, null);
}
return result;
}
@Override
public int update(Flight flight) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update Flight set Pnumber = ? , destin = ? , stime = ? where Pnm=?";
int result = 0;
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, flight.getPnumber());
pstmt.setString(2, flight.getDestin());
pstmt.setString(3, flight.getStime());
pstmt.setString(4, flight.getStime());
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally{
DBConnection.closeAll(conn, pstmt, null);
}
return result;
}
}
IFlightDao类
方法接口的封装
import java.util.List;
import po.Flight;
public interface IFlightDao {
//登录
public boolean findUser(String name,String passWord);
//查询所有信息
public List<Flight> findAll();
//根据起飞时间查询
public List<Flight> findByKey(String date);
//根据起飞时间查询
public List<Flight> findByKeyl(String aq);
//删除航班,通常是根据主键删除
public int delete(String id);
//更新航班 ,主键做条件修改其他项
public int update(Flight flight);
}
Flight类
对数据库要建立的数据进行封装,得到GET和SET方法
public class Flight {
private String Pnm ;//编号
private String Pnumber;//航班号
private String destin;//目的地
private String stime;//起飞日期
public Flight() {
super();
}
public Flight(String Pnm, String Pnumber, String destin, String stime) {
super();
this.Pnm = Pnm;
this.Pnumber = Pnumber;
this.destin = destin;
this.stime = stime;
}
public String getPnm() {
return Pnm;
}
public String getPnumber() {
return Pnumber;
}
public String getDestin() {
return destin;
}
public String getStime() {
return stime;
}
public void setPnm(String pnm) {
Pnm = pnm;
}
public void setPnumber(String pnumber) {
Pnumber = pnumber;
}
public void setDestin(String destin) {
this.destin = destin;
}
public void setStime(String stime) {
this.stime = stime;
}
@Override
public String toString() {
return "Flight [Pnm=" + Pnm + ", Pnumber=" + Pnumber + ", destin=" + destin
+ ", stime=" + stime + "]";
}
}
DBConnection类
用于连接数据库,如果在这里出现了问题,请看连接数据库的博客文章,就在我的主页里面,就会得到解决,并且有新的体会。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection {
public static Connection getConnection() {
Connection connection = null;
//链接sqlserver2014r2注册驱动程序字符串
String jdbc_driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//与数据库建立链接的字符串
String jdbc_url = "jdbc:sqlserver://localhost:1433;DatabaseName=Flight";
try {
//将JDBC驱动类装载入Java虚拟机
Class.forName(jdbc_driver);
//使用户名、密码与数据库建立链接,sa用户名,sa密码
connection = DriverManager.getConnection(jdbc_url,"sa","sa");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException ex) {
// TODO: handle exception
ex.printStackTrace();
}
return connection;
}
public static void closeAll(Connection conn,Statement stmt,ResultSet rs){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
接下来就是GUI设计页面,是采用的Netbeans的GUI设计器进行设计的,最终在IDEA上运行
Login类
登陆界面
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login extends JFrame {
private JLabel labelName;
private JLabel labelPwd;
private JTextField textName;
private JPasswordField passWord;
private JButton buttonConfirm;
private JButton buttonCancle;
private JPanel jplPane;
Login() {
initGUI();
showGUI();
}
void initGUI() {
jplPane = new JPanel();
jplPane.setBackground(Color.white);
this.getContentPane().add(jplPane);
jplPane.setLayout(null); //设置JPanel面板为空布局
JLabel labelName1 = new JLabel();
labelName1.setFont(new Font("宋体", Font.PLAIN, 14));
labelName1.setText("---欢迎使用航班管理系统---");
labelName1.setBounds(70, 30, 500, 20);
jplPane.add(labelName1);
labelName = new JLabel();
labelName.setFont(new Font("新宋体", Font.PLAIN, 14));
labelName.setText("用户名");
labelName.setBounds(65, 70, 50, 20);
jplPane.add(labelName);
textName = new JTextField();
textName.setBounds(115, 70, 120, 20);
jplPane.add(textName);
labelPwd = new JLabel();
labelPwd.setFont(new Font("新宋体", Font.PLAIN, 14));
labelPwd.setText("密码");
labelPwd.setBounds(65, 110, 50, 20);
jplPane.add(labelPwd);
passWord = new JPasswordField();
passWord.setBounds(115, 110, 120, 20);
jplPane.add(passWord);
buttonConfirm = new JButton("登录");
buttonConfirm.setBounds(85, 160, 60, 20);
buttonConfirm.setBackground(Color.green);
buttonCancle = new JButton("取消");
buttonCancle.setBounds(165, 160, 60, 20);
jplPane.add(buttonConfirm);
jplPane.add(buttonCancle);
//注册监听器
buttonConfirm.addActionListener(new ConfirmActionClass()); //new ConfirmActionClass()是监听器对象
}
//定义内部监听类
class ConfirmActionClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
String name = textName.getText(); //获取姓名
String pass = new String(passWord.getPassword());//获取密码,与下行代码功能相同
// String pass = String.copyValueOf(passWord.getPassword());
if (name.equals("admin") && pass.equals("123456")) {//判断姓名和密码是否正确
FlightJFrame2 flightJFrame2=new FlightJFrame2();
flightJFrame2.setExtendedState(JFrame.MAXIMIZED_HORIZ);
flightJFrame2.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, " 您输入的用户名或密码错误,请重新输入", "错误提示 ", JOptionPane.QUESTION_MESSAGE);
}
}
}
void showGUI() {
this.setVisible(true);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Login login = new Login();
login.setSize(350, 250);
}
}
接下来的代码有点多,估计看的时候也会不耐烦,想要的可以私聊我,全部免费,只为互相学习。就给大家上几个图片吧!看一下效果
主页面也是参照了 别人(灵魂漫步)的界面【现在好像没有了】 只不过它使用JAVAWEB 写出来的
这个便是纯JAVA编写,供大家参考,但其他界面均原创。
登录界面
主页面
想想还是不要再右边显示,虽然好看,但是对于初学最好是多页面,有助于理解事件处理功能,更加利于操作。
按日期查询
按目的地查询
删除航班
更新航班内容
希望大家通过交流更加精进自己的能力水平,谢谢大家观看,想要的私聊,直接发给大家源码