程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java+Mysql先生治理體系源碼

Java+Mysql先生治理體系源碼

編輯:關於JAVA

Java+Mysql先生治理體系源碼。本站提示廣大學習愛好者:(Java+Mysql先生治理體系源碼)文章只能為提供參考,不一定能成為您想要的結果。以下是Java+Mysql先生治理體系源碼正文


比來正在學java和數據庫,想起之前寫的先生治理體系,都是從網高低載,搪塞了事。閒來無事,也就本身寫了一個,不外功效完成的不是許多。 

開辟說話:java; 開辟情況:Mysql, java; 開辟對象:eclipse
開辟此案例,起首得在電腦上有java開辟情況和Mysql, java開辟情況與Mysql的搭建,就不再論述了,假如須要,請接洽我最上面的接洽方法:[email protected] 

此次體系比擬簡略單純:數據庫中只要一個表:stu;功效:可以或許對先生增長、刪除、修正。
開辟步調:
 1.在數據庫中建表: 

create table stu(
stuId String,
stuName String,
stuSex String,
stuAge int,
stuJG String,
stuDept Sring
);

2.java 代碼重要由四個類構成:
Test3包括主函數;StuModel用來刷新、出現數據庫;StuAddDiag用來完成增加讀者功效;StuUpDiag是修正先生信息。詳細代碼以下:
 Test3.java:

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test3 extends JFrame implements ActionListener {
 //界說一些控件
 JPanel jp1,jp2;
 JLabel jl1,jl2;
 JButton jb1,jb2,jb3,jb4;
 JTable jt;
 JScrollPane jsp;
 JTextField jtf;
 StuModel sm; 
 //界說銜接數據庫的變量
 Statement stat = null;
 PreparedStatement ps;
 Connection ct = null;
 ResultSet rs = null;
 
 public static void main(String[] args){
  Test3 test3 = new Test3();
 }
 //結構函數
 public Test3(){
  jp1 = new JPanel();
  jtf = new JTextField(10);
  jb1 = new JButton("查詢");
  jb1.addActionListener(this);
  jl1 = new JLabel("請輸出名字:");
  
  jp1.add(jl1);
  jp1.add(jtf);
  jp1.add(jb1);
  
  jb2 = new JButton("添加");
  jb2.addActionListener(this);
  jb3 = new JButton("修正");
  jb3.addActionListener(this);
  jb4 = new JButton("刪除");
  jb4.addActionListener(this);
  
  jp2 = new JPanel();
  jp2.add(jb2);
  jp2.add(jb3);
  jp2.add(jb4);
  
  //創立模子對象
  sm = new StuModel();
  
  
  //初始化
  jt = new JTable(sm);
  
  jsp = new JScrollPane(jt);
  
  //將jsp放入到jframe中
  this.add(jsp);
  this.add(jp1,"North");
  this.add(jp2,"South");
  this.setSize(600, 400);
  //this.setLocation(300, 200);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.setVisible(true);
 
 }
 public void actionPerformed(ActionEvent arg0) {
  //斷定是哪一個按鈕被點擊
  if(arg0.getSource() == jb1){
   System.out.println("用戶願望被查詢...");
  //由於把對表的數據封裝到StuModel中,可以比擬簡略的完成查詢
  String name = this.jtf.getText().trim();
  //寫一個sql語句
  String sql = "select * from stu where stuName = '"+name+"' ";
  //構建一個數據模子類,並更新
  sm = new StuModel(sql);
  //更新jtable
   jt.setModel(sm);
    
  }
  
  //1、彈出添加界面
  else if(arg0.getSource() == jb2){
  System.out.println("添加...");
  StuAddDiag sa = new StuAddDiag(this,"添加先生",true);
   
  //從新再取得新的數據模子,
  sm = new StuModel();
  jt.setModel(sm);
  }else if(arg0.getSource() == jb4){
  //2、刪除記載
  //1.獲得先生的ID
  int rowNum = this.jt.getSelectedRow();//getSelectedRow會前往給用戶點中的行
  //假如該用戶一行都沒有選,就前往-1
  if(rowNum == -1){
  //提醒
  JOptionPane.showMessageDialog(this, "請選中一行");
  return ;
  }
 //獲得學術ID
  String stuId = (String)sm.getValueAt(rowNum, 0);
  System.out.println("Id: "+stuId);
   
  //銜接數據庫,完成刪除義務
  try{
  //1.加載驅動
  Class.forName("com.mysql.jdbc.Driver"); 
  //2.銜接數據庫
  String url = "jdbc:mysql://localhost:3306/spdb1";
  String user = "root";
  String passwd = "lfdy";
    
  ct = DriverManager.getConnection(url, user, passwd);
  System.out.println("銜接勝利");
  ps = ct.prepareStatement("delete from stu where stuId = ?");
  ps.setString(1,stuId);
  ps.executeUpdate();
      
  }catch(Exception e){
   e.printStackTrace();
  }finally{
  try{
   if(rs!= null){
   rs.close();
   rs = null;
      
   }
   if(ps!= null){
   ps.close();
   ps = null;
   }
   if(ct != null){
   ct.close();
   ct = null;
   }
  } catch(Exception e){
   e.printStackTrace();
  }
   }
  sm = new StuModel();
 //更新jtable
  jt.setModel(sm); 
  }else if(arg0.getSource() == jb3){
  System.out.println("11111");
  //3、用戶願望修正
  int rowNum = this.jt.getSelectedRow();
  if(rowNum == -1){
  //提醒
  JOptionPane.showMessageDialog(this, "請選擇一行");
  return ;
  }
  //顯示對話框
  System.out.println( "12435");
 StuUpDiag su = new StuUpDiag(this, "修正學術", true, sm, rowNum);
 sm = new StuModel();
 jt.setModel(sm);
  } 
 }
}

StuModel.java: 

/*
 * 這是我的一個stu表的模子
 * 可以把對先生表的操作全都封裝到這個類
 */
package com.test2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.table.*;

public class StuModel extends AbstractTableModel{

 //rowData寄存行數據,columnNames寄存列名
 Vector rowData,columnNames;
   
 //界說銜接數據庫的變量
 Statement stat = null;
 Connection ct = null;
 ResultSet rs = null;
 
 //初始化
 public void init(String sql){
  if(sql.equals("")){
   sql = "select * from stu";
  }
  //中央
  //設置列名
  columnNames = new Vector();
  columnNames.add("學號");
  columnNames.add("名字");
  columnNames.add("性別");
  columnNames.add("年紀");
  columnNames.add("籍貫");
  columnNames.add("門派");
    
  //rowData寄存多行
  rowData = new Vector();
    
  try{
  //1.加載驅動
  Class.forName("com.mysql.jdbc.Driver");
  System.out.println("加載勝利");
  //2.銜接數據庫
  //界說幾個常量
  String url = "jdbc:mysql://localhost:3306/spdb1";
  String user = "root";
  String passwd = "lfdy";
     
  ct = DriverManager.getConnection(url,user,passwd);
  stat = ct.createStatement();//創立stat對象
  rs = stat.executeQuery(sql);//查詢成果
     
  while(rs.next()){
  Vector hang = new Vector();
  hang.add(rs.getString(1));
  hang.add(rs.getString(2));
  hang.add(rs.getString(3));
  hang.add(rs.getInt(4));
  hang.add(rs.getString(5));
  hang.add(rs.getString(6));
  //參加到rowData中
  rowData.add(hang);
      
  }
     
 }catch(Exception e){
  e.printStackTrace();
 }finally{
  try{
  if(rs!=null){
  rs.close();
  rs = null;
   }
  if(stat != null){
  stat.close();
  stat = null;
   }
  if(ct != null){
  ct.close();
  ct = null;
   }
    }catch(Exception e){
   e.printStackTrace();
   }
  }
 }
 
 //增長先生函數
 public void addStu(String sql){
  //依據用戶輸出的sql語句,完成添加義務
  
  
  
 }
 
 //第二個結構函數,經由過程傳遞的sql語句來取得數據模子
 public StuModel(String sql){
  this.init(sql);
 }
 
 //結構函數,用於初始化我的數據模子(表)
 public StuModel(){
  this.init("");
 }
 
 //獲得共有若干行
 public int getRowCount() {
  // TODO Auto-generated method stub
  return this.rowData.size();
 }

 //獲得共有若干列
 public int getColumnCount() {
  // TODO Auto-generated method stub
  return this.columnNames.size();
 }

 //獲得某行某列的數據
 public Object getValueAt(int row, int column) {
  // TODO Auto-generated method stub
  return ((Vector)(this.rowData.get(row))).get(column);
 }
 
 //獲得屬性名字
 public String getColumnName(int column) {
  // TODO Auto-generated method stub
  return (String)this.columnNames.get(column);
 }
}

StuAddDiag.java: 

package com.test2;

import javax.swing.JDialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;

public class StuAddDiag extends JDialog implements ActionListener {
 //界說我須要的swing組件
 JLabel jl1,jl2,jl3,jl4,jl5,jl6;
 JTextField jf1,jf2,jf3,jf4,jf5,jf6;
 JPanel jp1,jp2,jp3;
 JButton jb1,jb2;
 //owner代筆父窗口,title是窗口的名字,modal指定是形式窗口()或許非形式窗口
 public StuAddDiag(Frame owner,String title, boolean modal){
  //挪用父類辦法
  super(owner,title,modal);
  
  jl1 = new JLabel("學號");
  jl2 = new JLabel("名字");
  jl3 = new JLabel("性別"); 
  jl4 = new JLabel("年紀");
  jl5 = new JLabel("籍貫");
  jl6 = new JLabel("門派");
  
  jf1 = new JTextField(10);
  jf2 = new JTextField(10);
  jf3 = new JTextField(10);
  jf4 = new JTextField(10);
  jf5 = new JTextField(10);
  jf6 = new JTextField(10);
  
  jb1 = new JButton("添加");
  jb1.addActionListener(this);
  jb2 = new JButton("撤消");
  
  jp1 = new JPanel();
  jp2 = new JPanel();
  jp3 = new JPanel();
  
  //設置結構
  jp1.setLayout(new GridLayout(6,1));
  jp2.setLayout(new GridLayout(6,1));
  
  jp3.add(jb1);
  jp3.add(jb2);
  
  jp1.add(jl1);
  jp1.add(jl2);
  jp1.add(jl3);
  jp1.add(jl4);
  jp1.add(jl5);
  jp1.add(jl6);
  
  jp2.add(jf1);
  jp2.add(jf2);
  jp2.add(jf3);
  jp2.add(jf4);
  jp2.add(jf5);
  jp2.add(jf6);
  
  this.add(jp1, BorderLayout.WEST);
  this.add(jp2, BorderLayout.CENTER);
  this.add(jp3, BorderLayout.SOUTH);
  
  this.setSize(300,200);
  this.setVisible(true);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource() == jb1){
   Connection ct = null;
   PreparedStatement pstmt = null;
   ResultSet rs = null;
   
   try{
    //1.加載驅動
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("加載勝利");
    //2.銜接數據庫
    //界說幾個常量
    String url = "jdbc:mysql://localhost:3306/spdb1";
    String user = "root";
    String passwd = "lfdy";
    ct = DriverManager.getConnection(url,user,passwd);
  
    //與編譯語句對象
   
    String strsql = "insert into stu values(?,?,?,?,?,?)";
    pstmt = ct.prepareStatement(strsql);
    
    //給對象賦值
    pstmt.setString(1,jf1.getText());
    pstmt.setString(2,jf2.getText());
    pstmt.setString(3,jf3.getText());
    pstmt.setString(4,jf4.getText());
    pstmt.setString(5,jf5.getText());
    pstmt.setString(6,jf6.getText());
    
    pstmt.executeUpdate();
    
    this.dispose();//封閉先生對話框
    
   }catch(Exception arg1){
    arg1.printStackTrace();
   }finally{
    try{
    if(rs!=null){
    rs.close();
    rs = null;
     }
    if(pstmt != null){
    pstmt.close();
    pstmt = null;
     }
    if(ct != null){
    ct.close();
    ct = null;
     }    
    }catch(Exception arg2){
     arg2.printStackTrace();
    }
   }
   
  }
  
 }
 
 
}

StuUpDiag.java: 

package com.test2;
/*
 * 修正先生
 */
import javax.swing.JDialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;

public class StuUpDiag extends JDialog implements ActionListener {
 //界說我須要的swing組件
 JLabel jl1,jl2,jl3,jl4,jl5,jl6;
 JTextField jf1,jf2,jf3,jf4,jf5,jf6;
 JPanel jp1,jp2,jp3;
 JButton jb1,jb2;
 //owner代筆父窗口,title是窗口的名字,modal指定是形式窗口()或許非形式窗口
 public StuUpDiag(Frame owner,String title, boolean modal,StuModel sm,int rowNum){
  //挪用父類辦法
  super(owner,title,modal);
  
  jl1 = new JLabel("學號");
  
  jl2 = new JLabel("名字");
  
  jl3 = new JLabel("性別"); 
  jl4 = new JLabel("年紀");
  jl5 = new JLabel("籍貫");
  
  
  
  jl6 = new JLabel("門派");
  
  
  jf1 = new JTextField(10);jf1.setText((sm.getValueAt(rowNum, 0)).toString());
  jf2 = new JTextField(10);jf2.setText((String)sm.getValueAt(rowNum, 1));
  jf3 = new JTextField(10);jf3.setText(sm.getValueAt(rowNum, 2).toString());
  jf4 = new JTextField(10);jf4.setText((sm.getValueAt(rowNum, 3)).toString()); 
  jf5 = new JTextField(10);jf5.setText((String)sm.getValueAt(rowNum, 4));
  jf6 = new JTextField(10);jf6.setText((String)sm.getValueAt(rowNum, 5));
  
  jb1 = new JButton("修正");
  jb1.addActionListener(this);
  jb2 = new JButton("撤消");
  
  jp1 = new JPanel();
  jp2 = new JPanel();
  jp3 = new JPanel();
  
  //設置結構
  jp1.setLayout(new GridLayout(6,1));
  jp2.setLayout(new GridLayout(6,1));
  
  jp3.add(jb1);
  jp3.add(jb2);
  
  jp1.add(jl1);
  jp1.add(jl2);
  jp1.add(jl3);
  jp1.add(jl4);
  jp1.add(jl5);
  jp1.add(jl6);
  
  jp2.add(jf1);
  jp2.add(jf2);
  jp2.add(jf3);
  jp2.add(jf4);
  jp2.add(jf5);
  jp2.add(jf6);
  
  this.add(jp1, BorderLayout.WEST);
  this.add(jp2, BorderLayout.CENTER);
  this.add(jp3, BorderLayout.SOUTH);
  
  this.setSize(300,200);
  this.setVisible(true);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource() == jb1){
   Connection ct = null;
   PreparedStatement pstmt = null;
   ResultSet rs = null;
   
   try{
    //1.加載驅動
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("加載勝利");
    //2.銜接數據庫
    //界說幾個常量
    String url = "jdbc:mysql://localhost:3306/spdb1";
    String user = "root";
    String passwd = "lfdy";
    ct = DriverManager.getConnection(url,user,passwd);
  
    //與編譯語句對象
   
    String strsql = "insert into stu values(?,?,?,?,?,?)";
    pstmt = ct.prepareStatement(strsql);
    
    //給對象賦值
    pstmt.setString(1,jf1.getText());
    pstmt.setString(2,jf2.getText());
    pstmt.setString(3,jf3.getText());
    pstmt.setString(4,jf4.getText());
    pstmt.setString(5,jf5.getText());
    pstmt.setString(6,jf6.getText());
    
    pstmt.executeUpdate();
    
    this.dispose();//封閉先生對話框
    
   }catch(Exception arg1){
    arg1.printStackTrace();
   }finally{
    try{
    if(rs!=null){
    rs.close();
    rs = null;
     }
    if(pstmt != null){
    pstmt.close();
    pstmt = null;
     }
    if(ct != null){
    ct.close();
    ct = null;
     }    
    }catch(Exception arg2){
     arg2.printStackTrace();
    }
   }
   
  }
  
 }
 
 
}

開辟與測試成果:

1.體系主界面:

2.按名字查詢:

3.選中一行,刪除:

4.選中一行修正:

5.點擊添加按鈕,停止添加:

後續此體系將持續完美,有疑問和技巧交換的,可接洽自己:[email protected]

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved