基于JAVA的图书数据库管理-具有新增,修改,删除,查询功能

原文链接:https://my.oschina.net/u/2610176/blog/600952

结构类型JDBC-ODBC bridge plus ODBC driver;数据库采用Access。数据源是在ODBC设置好的“myBooK”。源程序如下:(最为练习,修改删除查询功能只留接口,没有编码实现)

import java.awt.*;
import java.awt.event.*;
//import java.beans.Statement;
import java.sql.*;
import javax.swing.*;


public class j03160301 extends JFrame{
 Connection ConnObj;
 Statement SQLStatement;
 ResultSet RS;
 
 JPanel contentPane;
 Panel panel1;
 Label L1,L2,L3,L4;
 TextField T1,T2,T3,T4;
 Button B1,B2,B3,B4,B5,B6,B7,B8,B9,B10;
 
 public j03160301(){
  contentPane=(JPanel)this.getContentPane();
  contentPane.setLayout(null);
  
  panel1=new Panel();
  //panel1.setBackground(BLACK);
  panel1.setBounds(0, 0, 380, 160);
  panel1.setLayout(null);
  panel1.setEnabled(false);
  
  contentPane.add(panel1);
  
  L1=new Label("ISBM: ");
  L1.setBounds(20, 40, 50, 25);
  panel1.add(L1);
  L2=new Label("bookname: ");
  L2.setBounds(20, 70, 50, 25);
  panel1.add(L2);
  L3=new Label("auther: ");
  L3.setBounds(20, 100, 50, 25);
  panel1.add(L3);
  L4=new Label("price: ");
  L4.setBounds(20, 130, 50, 25);
  panel1.add(L4);
  T1=new TextField();
  T1.setBounds(70, 40, 300, 25);
  T1.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e){
    T1_actionPerformed(e);
   }
  });
  panel1.add(T1);
  T2=new TextField();
  T2.setBounds(70, 70, 300, 25);
  T2.addActionListener(new myActionListener());
  panel1.add(T2);
  T3=new TextField();
  T3.setBounds(70, 100, 300, 25);
  T3.addActionListener(new myActionListener());
  panel1.add(T3);
  T4=new TextField();
  T4.setBounds(70, 130, 300, 25);
  T4.addActionListener(new myActionListener());
  panel1.add(T4);
  
  B1=new Button("第一条");
  B1.setBounds(new Rectangle(30,180,65,20));
  B1.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
    B1_mouseClicked(e);
   }
  });
  contentPane.add(B1);
  B2=new Button("上一条");
  B2.setBounds(new Rectangle(100,180,65,20));
  B2.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
    B2_mouseClicked(e);
   }
  });
  contentPane.add(B2);
  B3=new Button("下一条");
  B3.setBounds(new Rectangle(170,180,65,20));
  B3.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
    B3_mouseClicked(e);
   }
  });
  contentPane.add(B3);
  B4=new Button("最末条");
  B4.setBounds(new Rectangle(240,180,65,20));
  B4.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
    B4_mouseClicked(e);
   }
  });
  contentPane.add(B4); 
  B5=new Button("新增");
  B5.setBounds(new Rectangle(30,210,65,20));
  B5.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
    B5_mouseClicked(e);
   }
  });
  contentPane.add(B5);
  B6=new Button("修改");
  B6.setBounds(100, 210, 65, 20);
  B6.setEnabled(false);//因为这些功能暂时没有写。
  contentPane.add(B6);
  B7=new Button("删除");
  B7.setBounds(170, 210, 65, 20);
  B7.setEnabled(false);
  contentPane.add(B7);
  B8=new Button("查询");
  B8.setBounds(240, 210, 65, 20);
  B8.setEnabled(false);
  contentPane.add(B8);
  B9=new Button("确定");
  B9.setBounds(310, 180, 65, 20);
  B9.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
    B9_mouseClicked(e);
   }
  });
  contentPane.add(B9);
  
  B10=new Button("离开");
  B10.setBounds(310, 210, 65, 20);
  B10.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
    B10_mouseClicked(e);
   }
  });
  contentPane.add(B10);
  
  this.setBounds(200,100, 415, 280);
  this.setTitle("新增");
  this.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });
  this.setVisible(true);
  //===========================================
  //加载JDBC driver、连接数据库、送SELECT语句、返回结果集
  try
  {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载数据库驱动程序;
   ConnObj=DriverManager.getConnection("jdbc:odbc:myBook");//连接ODBC设置的myBook数据源;
   SQLStatement=ConnObj.createStatement(
     ResultSet.TYPE_SCROLL_INSENSITIVE,
     ResultSet.CONCUR_READ_ONLY);
   //其后由此Statement对象执行SQL指令时,返回的会是可卷动且制度的ResultSet对象
   RS=SQLStatement.executeQuery("SELECT * FROM BookData");
   //执行SELECT语句,会返回一个结果集
   //查询myBook数据源的BOOKDATA数据表全部字段的记录
   
   //必须先向下移到第一条;next(),因为一开始是指到第一条之前;
   if(RS.next())
   {
    //RS内至少有一条记录才会返回true,有一条记录才能读取这条记录个字段的数据;
    T1.setText(RS.getString("ISBN"));
    T2.setText(RS.getString("BookName"));
    T3.setText(RS.getString("Author"));
    T4.setText(String.valueOf(RS.getInt("Price")));
    //前三个字段都是文字,最后一个是长整数;
   
   }
   else
    JOptionPane.showMessageDialog(this, "myBook数据库内无任何数据");
  }catch(Exception ecp)
  {
   JOptionPane.showMessageDialog(this, ecp.getMessage());
  }
 }//public j03160301() end
 class myActionListener implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
   ((TextField)e.getSource()).transferFocus();//按下回车键,focus往下一个组件
  }
 }
 
 protected void T1_actionPerformed(ActionEvent e) {
  //新增时先检验该条是否已经存在
  if(T1.getText().trim().equals(""))
   return;
  String newISBN=T1.getText().trim();
  if(newISBN.length()>13)
  {
   JOptionPane.showMessageDialog(T1, "ISBN字段最大长度为13");
   return;
  }
  try
  {
   RS=SQLStatement.executeQuery(
     "SELECT * FROM BookData WHERE ISBN='"+newISBN+"'");
   if(RS.next())
   {
    T1.setText(RS.getString("ISBN"));
    T2.setText(RS.getString("BookName"));
    T3.setText(RS.getString("Author"));
    T4.setText(String.valueOf(RS.getInt("Price")));
    JOptionPane.showMessageDialog(T1, "此笔数据已存在");
    
   }
   else{//不得再更改新的一条ISBN
    panel1.setEnabled(true);
    T1.setText(newISBN);
    T1.setEditable(true);
    T2.setEnabled(true);
    T3.setEnabled(true);
    T4.setEnabled(true);
    B9.setEnabled(true);
    T2.requestFocus();
   }
  }catch(SQLException o){System.out.println(o.getMessage());}
  
 }

 void B1_mouseClicked(MouseEvent e)
 {
  try
  {
   RS.first();//move to the first item
   T1.setText(RS.getString("ISBN"));
   T2.setText(RS.getString("BookName"));
   T3.setText(RS.getString("Author"));
   T4.setText(String.valueOf(RS.getInt("Price")));
  }catch(SQLException ecp){}
 }//B1_mouseClicked(MouseEvent e) end;
 
 void B2_mouseClicked(MouseEvent e)
 {
  try
  {
   if(RS.previous())
   {//move to the previous item
    T1.setText(RS.getString("ISBN"));
    T2.setText(RS.getString("BookName"));
    T3.setText(RS.getString("Author"));
    T4.setText(String.valueOf(RS.getInt("Price")));
   }
   else
   {
    //返回false表示移到了第一条之前
    RS.first();//再移到第一条;
    JOptionPane.showMessageDialog(B2, "移到第一条");
    //不需要改变T1-T4的文字,因为刚才已经显示第一条内容
   }
  }catch(SQLException ecp){}
 }//B2_mouseClicked(MouseEvent e) end;
 
 void B3_mouseClicked(MouseEvent e)
 {
  try
  {
   if(RS.next())
   {//move to the next item
    T1.setText(RS.getString("ISBN"));
    T2.setText(RS.getString("BookName"));
    T3.setText(RS.getString("Author"));
    T4.setText(String.valueOf(RS.getInt("Price")));
   }
   else
   {
    //返回false表示移到了最末条之后
    RS.last();//再移到最末条;
    JOptionPane.showMessageDialog(B3, "移到最末条");
    //不需要改变T1-T4的文字,因为刚才已经显示第一条内容
   }
  }catch(SQLException ecp){}
 }//B3_mouseClicked(MouseEvent e) end;
 
 void B4_mouseClicked(MouseEvent e)
 {
  try
  {
   RS.last();//move to the last item
   T1.setText(RS.getString("ISBN"));
   T2.setText(RS.getString("BookName"));
   T3.setText(RS.getString("Author"));
   T4.setText(String.valueOf(RS.getInt("Price")));
  }catch(SQLException ecp){}
 }//B4_mouseClicked(MouseEvent e) end;
 
 void B5_mouseClicked(MouseEvent e)
 {
  T1.setText(" ");
  T2.setText(" ");
  T3.setText(" ");
  T4.setText(" ");
  panel1.setEnabled(true);
  T2.setEnabled(false);
  T3.setEnabled(false);
  T4.setEnabled(false);
  B1.setEnabled(false);
  B2.setEnabled(false);
  B3.setEnabled(false);
  B4.setEnabled(false);
  B5.setEnabled(false);
  B9.setEnabled(true);
  B10.setEnabled(true);
  /*try
  {
   RS.last();//move to the last item
   T1.setText(RS.getString("ISBN"));
   T2.setText(RS.getString("BookName"));
   T3.setText(RS.getString("Author"));
   T4.setText(String.valueOf(RS.getInt("Price")));
  }catch(SQLException ecp){}*/
 }//B4_mouseClicked(MouseEvent e) end;
  void B9_mouseClicked(MouseEvent e) {//确认插入按钮;
  ToInsert();
 }
 private void ToInsert() {
  int checkPrice;
  if(T2.getText().trim().equals("")||T3.getText().trim().equals("")||T4.getText().trim().equals("")||T2.getText().trim().length()>50||T3.getText().trim().length()>30)
  {
   JOptionPane.showMessageDialog(B9, "有数据尚未填正确的值");
   return;
  }
  try//检查价格
  {
   checkPrice=Integer.parseInt(T4.getText().trim());
  }catch(NumberFormatException o)
  {
   JOptionPane.showMessageDialog(B9, "价格请填入整数");
   return;
  }
  
  String insertSQL="INSERT INTO BookData(ISBN,BookName,Author,Price)"+"VALUES('"+T1.getText().trim()+"','"+T2.getText().trim()
  +"','"+T3.getText().trim()+"','"+T4.getText().trim()+"')";
  try
  {
   System.out.println(insertSQL);
   SQLStatement.executeUpdate(insertSQL);
  }catch(SQLException o){
   JOptionPane.showMessageDialog(B9, o.getMessage());
  }
  try
  {
   RS=SQLStatement.executeQuery("SELECT *FROM BookData");
   RS.last();//移动到最后一条,因为增加的数据会加到数据库的最后一条后面;
   T1.setText(RS.getString("ISBN"));
   T2.setText(RS.getString("BookName"));
   T3.setText(RS.getString("Author"));
   T4.setText(String.valueOf(RS.getInt("Price")));
  }catch(SQLException o){JOptionPane.showMessageDialog(B9, o.getMessage());}
  
  
 }

  void B10_mouseClicked(MouseEvent e) {//清除按钮;
  try
  {
   RS=SQLStatement.executeQuery("SELECT * FROM BookData");
   if(RS.next())
   {
    T1.setText(RS.getString("ISBN"));
    T2.setText(RS.getString("BookName"));
    T3.setText(RS.getString("Author"));
    T4.setText(String.valueOf(RS.getInt("Price")));
   }
  }catch(SQLException o)
  {JOptionPane.showMessageDialog(B10,o.getMessage());}
  //控制画面操作过程
  panel1.setEnabled(true);
  T1.setEnabled(true);
  T2.setEnabled(true);
  T3.setEnabled(true);
  T4.setEnabled(true);
  B1.setEnabled(true);
  B2.setEnabled(true);
  B3.setEnabled(true);
  B4.setEnabled(true);
  B5.setEnabled(true);
  B9.setEnabled(false);
  B10.setEnabled(false);
 }
 
 
 public static void main(String[] args)
 {
  new j03160301();
 }

}

转载于:https://my.oschina.net/u/2610176/blog/600952

上一篇:Java录制音频、播放音频、保存音频


下一篇:Qt 蓝牙开发 客户端