Java之数据库连接(二)

来源:互联网 发布:linux sync用户 编辑:程序博客网 时间:2024/04/30 07:05

Java+MySql:学生图书信息管理系统

       该程序片段改进了Java之数据库连接(一)的方法,并在此基础上实现了一个小型的基于GUI的学生图书管理系统,实现了学生借阅图书信息的增加、删除、更新、查看功能。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/***@Program ConnDB:【1】连接数据库(连接MYSQL)*@Author: Livjunjie*@Time:   2013年10月20日17:03:47*/package cn.xuc.edu.JDBC;import java.sql.*;import java.util.Vector;public class ConnDB {public static void main(String[] args) {// TODO Auto-generated method stubConnection conn=null;try {//连接mysqlClass.forName("com.mysql.jdbc.Driver");    String url="jdbc:mysql://localhost:3306/book";String user="root";String password="123456"; conn=DriverManager.getConnection(url,user,password);System.out.println("数据库连接成功");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockSystem.out.println("数据库驱动没有找到");e.printStackTrace()} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.println("数据库连接失败");e.printStackTrace();}finally{if(conn!=null)try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.println("数据库关闭失败");e.printStackTrace();}}}/***@Program SQLDemo: 【2】加载驱动程序*@Author: Livjunjie*@Time:   2013年10月20日17:06:21*/package cn.xuc.edu.JDBC;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Vector;public class SQLDemo {public static Vector queryData(String sql )  {// TODO Auto-generated method stub// TODO Auto-generated method stubConnection conn=null;                Vector data=new Vector();try {//连接mysqlClass.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://localhost:3306/book";String user="root";String password="123456";conn=DriverManager.getConnection(url,user,password);System.out.println("数据库连接成功");//创建StatementStatement stmt=conn.createStatement();//执行sql语句    sql="select * from book";ResultSet rs=stmt.executeQuery(sql);//处理查询结果while(rs.next()){Vector line=new Vector();for(int i=1;i<=6;i++){//System.out.println(rs.getObject(i));line.add(rs.getObject(i));}data.add(line);}rs.close();stmt.close();conn.close();//System.out.println(data);return data;} catch (ClassNotFoundException e) {System.out.println("数据库驱动没有找到");e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.println("数据库连接失败");e.printStackTrace();}finally{if(conn!=null)try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.println("数据库关闭失败");e.printStackTrace();}}return null;}    //添加数据public static boolean insertData1(){Connection conn=null; try {Statement stmt=conn.createStatement();//写插入数据的SQL语句  这儿应该怎么写呢?String sql="select * from book";//执行插入数据的SQL语句,返回受影响的行数int rs=stmt.executeUpdate(sql);stmt.close();conn.close();               //如果受影响的行数大于零,则插入数据成功,返回true,否则插入数据失败,返回false.if(rs>0){return true;}else{return false;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}}/***@Program LoginUI:【3】GUI窗口程序代码*@Author: Livjunjie*@Time:   2013年10月20日17:17:29*/package cn.xuc.edu.JDBC;import java.awt.Color;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.util.Vector;import javax.swing.ButtonGroup;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;import javax.swing.table.JTableHeader;public class LoginUI extends javax.swing.JFrame {int index=0;/** Creates new form Lgin */DefaultTableModel dtm=new DefaultTableModel();JTable tblStudent=new JTable(dtm);Vector data=new Vector();Vector title=new Vector();JTableHeader jTableHeader1=tblStudent.getTableHeader();public Login2() {initComponents();}private void initComponents() {        jLabel1 = new javax.swing.JLabel();jTextField1 = new javax.swing.JTextField();jLabel2 = new javax.swing.JLabel();jTextField2 = new javax.swing.JTextField();jLabel3 = new javax.swing.JLabel();jTextField3 = new javax.swing.JTextField();jLabel4 = new javax.swing.JLabel();jTextField4 = new javax.swing.JTextField();jLabel5 = new javax.swing.JLabel();jTextField5 = new javax.swing.JTextField();jLabel6 = new javax.swing.JLabel();jRadioButton1 = new javax.swing.JRadioButton();jRadioButton2 = new javax.swing.JRadioButton();jButton1 = new javax.swing.JButton();jButton2 = new javax.swing.JButton();jButton3 = new javax.swing.JButton();jButton4 = new javax.swing.JButton();jButton5 = new javax.swing.JButton();    jButton6 = new javax.swing.JButton();jButton7 = new javax.swing.JButton();jButton8 = new javax.swing.JButton();jScrollPane1 = new javax.swing.JScrollPane();jTable1 = new javax.swing.JTable();       setTitle("学生图书信息管理系统");                  setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);    tblStudent.setBounds(new Rectangle(12,250,515,163));jTableHeader1.setBounds(new Rectangle(12,200,500,0));this.add(tblStudent);this.add(jTableHeader1);//调用在JTable中显示数据的方法showTableData();//调用在JTextFiled中显示数据的方法showJTextFiledData(index);       jLabel1.setText("\u56fe\u4e66\u7f16\u53f7");jLabel2.setText("\u56fe\u4e66\u540d\u79f0");jLabel3.setText("\u4f5c\u8005");jLabel4.setText("\u51fa\u7248\u793e");jLabel5.setText("\u51fa\u7248\u65e5\u671f");jLabel6.setText("\u5728\u5e93\u72b6\u6001");jRadioButton1.setText("\u5728\u5e93");jRadioButton2.setText("\u501f\u51fa");jButton1.setText("<<");            jButton1.addActionListener(new   java.awt.event.ActionListener() {Public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton1ActionPerformed(evt);}});jButton2.setText("<");jButton2.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton2ActionPerformed(evt);}});jButton3.setText(">");jButton3.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton3ActionPerformed(evt);}});jButton4.setText(">>");jButton4.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton4ActionPerformed(evt);}});jButton5.setText("\u6dfb\u52a0");jButton5.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton5ActionPerformed(evt);}});jButton6.setText("\u4fee\u6539");jButton6.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton6ActionPerformed(evt);}});jButton7.setText("\u5220\u9664");jButton7.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton7ActionPerformed(evt);}});jButton8.setText("\u4fdd\u5b58");jButton8.addActionListener(new  java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent{ jButton8ActionPerformed(evt);}});  //注意此处的代码jScrollPane1.setViewportView(tblStudent);javax.swing.GroupLayoutlayout=new javax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING.addComponent(jScrollPane1,javax.swing.GroupLayout.Alignment.LEADING,javax.swing.GroupLayout.DEFAULT_SIZE,420,Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.LEADINlayout.createSequentialGroup().addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,89,javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.createSequentialGroup().addComponent(bg.add(jRadioButton1);        bg.add(jRadioButton2);        pack();        this.setLocationRelativeTo(null);}// </editor-fold>private void showJTextFiledData(int index) {// TODO Auto-generated method stub//调用查询数据获得的方法data=SQLDemo.queryData("Select *from book");//判断如果获取数据的数量大于零则显示数据if(data.size()>0){//实例化一个书类的对像            Book bk=new Book();//取出data中索引为index的数据Vector vBk=(Vector)data.get(index);//调用获Book类的获取某本书的信息的方法bk.getBookInfo(vBk);//将书籍编号显示在书籍编号文本框jTextField1.setText(bk.bkID);jTextField2.setText(bk.bkName);jTextField3.setText(bk.bkAuthor);jTextField4.setText(bk.bkPress);jTextField5.setText(bk.bkPDat);//判断在库状态是在库,则将在库按钮选中if(bk.bkStatus.equals("在库")){jRadioButton1.setSelected(true);    }else{            //如果状态是借出,则将借出按钮选中jRadioButton2.setSelected(true);}}}           //GEN-END:initComponentsprivate void showTableData() { // TODO Auto-generated method stub        //准备表头中的数据title.add("图书编号");title.add("图书名称");title.add("作者");title.add("出版社");title.add("出版日期");title.add("在库状态");//调用查询数据获得的方法data=SQLDemo.queryData("Select *from book");//使用默认表格模式dtm在表格中显示数据dtm.setDataVector(data,title);}PrivatevoidjButton8ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:}privatevoidjButton7ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code herprivatevoidjButton6ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:}    privatevoidjButton5ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:    }privatevoidjButton4ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:index=data.size()-1;showJTextFiledData(index);}private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:index++;if(index<data.size())showJTextFiledData(index);}privatevoid jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:index--;if(index>=0)showJTextFiledData(index);}private void jButton1ActionPerformed(ActionEvent e) {// TODO add your handling code here:index=0;showJTextFiledData(index);}/** * @param args the command line arguments */public static void main(String args[]) {java.awt.EventQueue.invokeLater(new Runnable() {public void run() {new Login2().setVisible(true);}});}//GEN-BEGIN:variables// Variables declaration - do not modifyprivate javax.swing.JButton jButton1;private javax.swing.JButton jButton2;private javax.swing.JButton jButton3;private javax.swing.JButton jButton4;private javax.swing.JButton jButton5;private javax.swing.JButton jButton6;private javax.swing.JButton jButton7;private javax.swing.JButton jButton8;    private javax.swing.JLabel jLabel1;private javax.swing.JLabel jLabel2;private javax.swing.JLabel jLabel3;private javax.swing.JLabel jLabel4;private javax.swing.JLabel jLabel5;private javax.swing.JLabel jLabel6;private javax.swing.JRadioButton jRadioButton1;private javax.swing.JRadioButton jRadioButton2;private javax.swing.JScrollPane jScrollPane1;private javax.swing.JTable jTable1;private javax.swing.JTextField jTextField1;private javax.swing.JTextField jTextField2;private javax.swing.JTextField jTextField3;private javax.swing.JTextField jTextField4;private javax.swing.JTextField jTextField5; //实例化单选按钮组    ButtonGroup bg=new ButtonGroup();     //定义数据索引     int indexd=0;// End of variables declaration//GEN-END:variables}
/***@Program Application:启动应用程序*@Author: Livjunjie*@Time: 2013年10月20日17:54:36*/package cn.xuc.edu.JDBC;public class Application {/** * @param args */public static void main(String[] args){              // TODO Auto-generated method stubnew               LoginUI().setVisible(true);       }}
程序运行结果示例图:
(1)图书借出状态
(2)图书在库状态
原创粉丝点击