posts - 89, comments - 241, trackbacks - 0, articles - 1
             :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

          學生管理系統 源代碼

          Posted on 2009-06-08 09:13 saobaolu 閱讀(1235) 評論(1)  編輯  收藏 所屬分類: java基礎與算法
          http://www.aygfsteel.com/chenglu/archive/2009/06/08/Student_Manager.html
          DB.java
          /*
          * To change this template, choose Tools | Templates
          * and open the template in the editor.
          */

          package opera.DB;
          import java.sql.*;
          import java.util.logging.Level;
          import java.util.logging.Logger;
          /**
          *
          *
          @author soft
          */
          public class DB {
             
          private String sName;
             
          private String Sno;
             
          private String Sgrade;
             
          private String Ssex;
             
          private String QSql;
             
          private Statement stm=null;
             
          private ResultSet rs = null;
             
          private int i =0;
             
          private String ex;
             
          private String usn;
             
          private String pwd;
             
             
          public Connection getCon(){
               
          Connection con=null;
               
          try {
                 
          Class.forName("com.mysql.jdbc.Driver");
                   con = DriverManager.getConnection
          ("jdbc:mysql://localhost:3306/stu","root","");
               
          } catch (SQLException ex) {
                 
          ex.printStackTrace();
               
          } catch (ClassNotFoundException ex) {
                 
          ex.printStackTrace();
               
          }
               
          return con;
             
          }
             
          /**
               *
          @return the sName
               */
             
          public String getSName() {
                 
          return sName;
             
          }

             
          /**
               *
          @param sName the sName to set
               */
             
          public void setSName(String sName) {
                 
          this.sName = sName;
             
          }

             
          /**
               *
          @return the Sno
               */
             
          public String getSno() {
                 
          return Sno;
             
          }

             
          /**
               *
          @param Sno the Sno to set
               */
             
          public void setSno(String Sno) {
                 
          this.Sno = Sno;
             
          }

             
          /**
               *
          @return the Sgrade
               */
             
          public String getSgrade() {
                 
          return Sgrade;
             
          }

             
          /**
               *
          @param Sgrade the Sgrade to set
               */
             
          public void setSgrade(String Sgrade) {
                 
          this.Sgrade = Sgrade;
             
          }
             
          /**
               *
          @return the Ssex
               */
                 
          public String getSsex() {
                 
          return Ssex;
             
          }

             
          /**
               *
          @param Ssex the Ssex to set
               */
             
          public void setSsex(String Ssex) {
                 
          this.Ssex = Ssex;
             
          }


             
          public int DBinsert(int choice) throws SQLException{
                 
          //Connection con =null;

                      //Class.forName("com.mysql.jdbc.Driver");
                      //con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stu","root","");
                     
          stm = getCon().createStatement();
                     
          if(choice==1){
                     
          i = stm.executeUpdate("insert into stu values('"+getSno()+"','"+getSName()+"','"+getSsex()+"','"+getSgrade()+"')");
                     
          }
                     
          else if(choice==2){
                     
          i = stm.executeUpdate("insert into user values('"+getUsn()+"','"+getPwd()+"')");
                     
          }//關閉
                     
          getCon().close();
                      stm.close
          ();
                     stm =
          null;
                 
          return i;
             
          }
             
          public void DBQuery(int check) throws SQLException{//throws Exception{
            
                //ResultSet rs = null;
                //Statement stm = null;
               
          stm = getCon().createStatement();
                rs = stm.executeQuery
          (getQSql());
               
          //select * from stu where sno = getSno()getQSql()
               
          if(check==1){
                 
          //獲取
               
          while(rs.next()){
               
          setSno(rs.getString("sno"));
                setSName
          (rs.getString("sName"));
                setSsex
          (rs.getString("ssex"));
                setSgrade
          (rs.getString("sGrade"));
               
          }
               
          /*關閉
                getCon().close();
                  stm.close();
                  stm = null;
                rs.close();
                rs = null;
          */         
               
          }
               
          else if(check==2){
                 
          while(rs.next()){
                 
          setPwd(rs.getString("pwd"));
                  setUsn
          (rs.getString("usn"));
               
          }
                }
              }
             
             
          public int DBUpdate(){
             
               
          try {
               
          //Statement stm = null;
               
          stm = getCon().createStatement();
                String sq =
          "update stu set sName = '"+getSName()+"',ssex = '"+getSsex()+"',sGrade = '"+getSgrade()+"'where sno ='"+getSno()+"'";
                i = stm.executeUpdate
          (sq);
               
          //UPDATE Store_Information SET Sales = 500 WHERE store_name = "Los Angeles"  AND Date = "Jan-08-1999"
                //關閉
               
          getCon().close();
                stm.close
          ();
                stm =
          null;
             
          } catch (SQLException ex) {
               
          Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
             
          }
             
          return i;
             
          }
             
          public int DBdelete(){
             
          //Statement stm = null;
             
          try {
               
          getCon().createStatement();
                String dsql=
          "DELETE FROM stu WHERE sno = '"+getSno()+"'";
               
          //DELETE FROM Store_Information WHERE store_name = "Los Angeles"
                //娘啊,user里的表忘刪除了……暈……2009.6.4 18:50 銘記!
               
          stm.executeUpdate("delete from user where usn = '"+getSno()+"'");
                i = stm.executeUpdate
          (dsql);
               
          //關閉
               
          getCon().close();
                stm.close
          ();
                stm =
          null;
             
          } catch (SQLException ex) {
               
          ex.printStackTrace();
             
          }
             
          return i;
             
          }
           
          /**
             *
          @return the QSql
             */
           
          public String getQSql() {
             
          return QSql;
           
          }

           
          /**
             *
          @param QSql the QSql to set
             */
           
          public void setQSql(String QSql) {
             
          this.QSql = QSql;
           
          }

           
          /**
             *
          @return the usn
             */
           
          public String getUsn() {
             
          return usn;
           
          }

           
          /**
             *
          @param usn the usn to set
             */
           
          public void setUsn(String usn) {
             
          this.usn = usn;
           
          }

           
          /**
             *
          @return the pwd
             */
           
          public String getPwd() {
             
          return pwd;
           
          }

           
          /**
             *
          @param pwd the pwd to set
             */
           
          public void setPwd(String pwd) {
             
          this.pwd = pwd;
           
          }
          }

          完整NetBeans項目代碼:
          /Files/chenglu/Student_NetBeana_Src.zip


          沒有所謂的命運,只有不同的選擇!
          主站蜘蛛池模板: 寿光市| 嫩江县| 马公市| 谷城县| 牡丹江市| 隆安县| 桃江县| 武邑县| 利川市| 青川县| 渝北区| 肃北| 蓬溪县| 温宿县| 靖江市| 巴青县| 靖宇县| 辉县市| 江油市| 犍为县| 始兴县| 义马市| 凌海市| 宝兴县| 东明县| 海门市| 靖江市| 怀仁县| 新竹县| 通渭县| 遂川县| 寿宁县| 宁安市| 涪陵区| 山西省| 商水县| 曲靖市| 启东市| 济源市| 昌宁县| 湟中县|