Using Java object-oriented program operation Oralce database to achieve interface increase, delete, change, check __c#

Source: Internet
Author: User
Tags gettext
Creating Oracle Scripts

System interface and code one, Stumanage.java main interface, the default display and query out students

Package stumanage;
Import javax.swing.*;
Import java.awt.*;

Import java.awt.event.*;
    public class Stumanager extends JFrame implements actionlistener{JPanel;
    JLabel Jlabel1;
    JTextField Jtextfield1;
    JButton Jbutton1,jbutton2,jbutton3,jbutton4;
    JTable Jtable1;
    JScrollPane jsp = null;

    Stumodel sm = null;
        Stumanager () {this.setlayout (new GridLayout (3,1));
        Layout line Jpanel1 = new JPanel ();
        Jlabel1 = new JLabel ("username");
        Jtextfield1 = new JTextField (10);
        Jbutton1 = new JButton ("Query");
        Jbutton1.addactionlistener (this);
        Jpanel1.add (JLABEL1);
        Jpanel1.add (JTEXTFIELD1);

        Jpanel1.add (Jbutton1);
        Layout two line sm = new Stumodel ();
        String[] Paras = {"1"};
        Sm.querystu ("SELECT * from student where 1=?", paras);
        Jtable1 = new JTable (SM);

        JSP = new JScrollPane (JTABLE1);
        Layout three lines Jpanel2 = new JPanel (); Jbutton2 = new JButton ("add");
        Jbutton2.addactionlistener (this);
        Jbutton3 = new JButton ("Modify");
        Jbutton3.addactionlistener (this);
        Jbutton4 = new JButton ("delete");
        Jbutton4.addactionlistener (this);
        Jpanel2.add (Jbutton2);
        Jpanel2.add (Jbutton3);

        Jpanel2.add (JBUTTON4);
        This.add (Jpanel1,borderlayout.north);
        This.add (JSP);
    This.add (Jpanel2,borderlayout.south);
        }//main function public static void main (string[] args) {Stumanager stu = new Stumanager ();
        Stu.settitle ("Student Information Management system (Li Yandong)");
        Stu.setsize (350,300);
        Stu.setdefaultcloseoperation (Jframe.exit_on_close);
    Stu.setvisible (TRUE);
            @Override public void actionperformed (ActionEvent e) {//query if (E.getsource () ==jbutton1) {
            The data of the table is encapsulated into Stumodel, and the query String sql = "" is completed.
            String name = This.jtextfield1.getText (). Trim (); if (name!=null &&!name.trim (). Equals ("")){sql = "select * from student where stuname=?";
                else{Joptionpane.showmessagedialog (jbutton1, "input condition", "Hint:", 1);
            return;
            //Build the new data model and update SM = new Stumodel ();
            String[] paras = {name};
            Sm.querystu (SQL, paras);
        This.jtable1.setModel (SM);

            //Add student else if (E.getsource () ==jbutton2) {Stuadd add = new Stuadd (this, add student, true);
            * Add complete re-get model, same as other operations SM = new Stumodel ();
            String[] Paras = {"1"};
            Sm.querystu ("SELECT * from student where 1 =?", paras);

        This.jtable1.setModel (SM);
            //Modify Student else if (E.getsource () ==jbutton3) {int rownum = This.jtable1.getSelectedRow ();
            System.out.println ("Modify the first" + (rownum+1) + "line");
                if (rownum = = 1) {Joptionpane.showmessagedialog (this, "Please select Modified row");return;
            New Stuupdate (This, "modify student", True, SM, rownum);

            SM = new Stumodel ();
            String[] Paras = {"1"};

            Sm.querystu ("SELECT * from student where 1 =?", paras);
        This.jtable1.setModel (SM);
            //Delete student else if (E.getsource () ==jbutton4) {//1. Get the student's Id,getselectedrow returns a row in the user's point, or 1 if one row is not selected
            int rownum = This.jtable1.getSelectedRow ();
                if (rownum = = 1) {Joptionpane.showmessagedialog (this, "Please select the student to delete");
            return;
            //Get the student id String stuid = (string) sm.getvalueat (rownum, 0);
            String sql = "Delete from student where stuid=?";
            String[] Paras ={stuid};
            SM = new Stumodel ();
            String[] PARAS1 ={"1"};
        Sm.updatestu (SQL, paras);
            Sm.querystu ("SELECT * from student where 1=?", PARAS1);
            This.jtable1.setModel (SM); joptionpane.shOwmessagedialog (This, "delete success"); }
    }
}
Second, Stumodel.java * Data operation model Interface (modify, delete students)
Package stumanage;
Import Java.sql.ResultSet;
Import Java.util.Vector;

Import Javax.swing.table.AbstractTableModel;

    public class Stumodel extends abstracttablemodel{Vector rowdata,columnnames;
        public boolean Updatestu (String sql,string[] paras) {Studao ss = new Studao ();
    return ss.update (SQL, paras);
        public void Querystu (String sql,string[] paras) {Studao he = null;
        Table middle ColumnNames = new Vector ();
        Columnnames.add ("School Number");
        Columnnames.add ("name");
        Columnnames.add ("gender");
        Columnnames.add ("Age");
        Columnnames.add ("Native Place");
        Columnnames.add ("department");  RowData = new Vector ();
            RowData can store multiple lines try{he = new Studao ();

            ResultSet rs = he.query (SQL, paras);
                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));
            Add to RowData rowdata.add (hang);
        }}catch (Exception e) {e.printstacktrace ();
        }finally{He.close ();
    @Override public int GetRowCount () {//Get the number of columns return this.rowData.size ();
    @Override public int getColumnCount () {//Gets the number of rows return this.columnNames.size (); @Override public Object getvalueat (int row, int column) {//Get the specified row, column data return ((Vector) This.row
    Data.get (Row)). get (column);
    @Override public String getcolumnname (int column) {return (String) this.columnNames.get (column); }
}
Third, Stuadd.java Add/Modify Student interface
The change time School number is the ash state
Package stumanage;
Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
    public class Stuadd extends JDialog implements ActionListener {JLabel jl1,jl2,jl3,jl4,jl5,jl6;
    JTextField Jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;
    JButton Jbutton1,jbutton2;
    JPanel Jpanel1,jpanel2,jpanel3;
        Parent window, window name, * Modal public Stuadd (Frame owner,string Title,boolean modal) {super (owner,title,modal);
        Jpanel1 = new JPanel (new GridLayout (6,1,1,1));
        JL1 = new JLabel ("School Number");
        JL2 = new JLabel ("name");
        Jl3 = new JLabel ("gender");
        Jl4 = new JLabel ("Age");
        JL5 = new JLabel ("Native Place");
        JL6 = new JLabel ("the System");
        Jpanel1.add (JL1);
        Jpanel1.add (JL2);
        Jpanel1.add (JL3);
        Jpanel1.add (JL4);
        Jpanel1.add (JL5);

        Jpanel1.add (JL6);
        Jpanel2 = new JPanel (new GridLayout (6,1,1,1));
        JTF1 = new JTextField (10);
        JTF2 = new JTextField (10); Jtf3 = new JTextField (10);
        JTF4 = new JTextField (10);
        Jtf5 = new JTextField (10);
        Jtf6 = new JTextField (10);
        Jpanel2.add (JTF1);
        Jpanel2.add (JTF2);
        Jpanel2.add (JTF3);
        Jpanel2.add (JTF4);
        Jpanel2.add (JTF5);

        Jpanel2.add (JTF6);
        Jpanel3 = new JPanel ();
        Jbutton1 = new JButton ("Save");
        Jbutton1.addactionlistener (this);
        Jbutton2 = new JButton ("Cancel");
        Jpanel3.add (Jbutton1);

        Jpanel3.add (Jbutton2);
        This.add (jpanel1,borderlayout.west);
        This.add (Jpanel2,borderlayout.east);
        This.add (Jpanel3,borderlayout.south);
        This.settitle ("Add student Information");
        This.setsize (200, 200);
    This.setvisible (TRUE);  @Override public void actionperformed (ActionEvent e) {if (E.getsource () ==jbutton1) {Stumodel
            temp = new Stumodel ();
            String sql = "INSERT into student values (?,?,?,?,?,?)"; String[] Paras = {Jtf1.gettext (),Jtf2.gettext (), Jtf3.gettext (), Jtf4.gettext (), Jtf5.gettext (), Jtf6.gettext ()};
            if (!temp.updatestu (SQL, paras)) {Joptionpane.showmessagedialog (this, add failed);
                } else{This.setvisible (false);
            Joptionpane.showmessagedialog (This, "add success");

        //Close dialog box This.dispose (); }
    }
}
Iv. Studao.java//operation of Oracle database entity classes, adding, deleting, and modifying operations merging
Package stumanage;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;

Import Java.sql.ResultSet;
    public class Studao {Connection con =null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    String Driver = "Oracle.jdbc.driver.OracleDriver";
    String url = "Jdbc:oracle:thin: @localhost: 1521:study";
    String userName = "admin";

    String PassWord = "admin";
        Public ResultSet query (String sql) {try{class.forname (driver);
            con = drivermanager.getconnection (Url,username,password);
            pstm = con.preparestatement (sql);
        rs = Pstm.executequery ();
        catch (Exception e) {e.printstacktrace ();
    }finally{//temporarily cannot close} return RS;
        Public ResultSet query (String sql,string[] paras) {try{class.forname (driver);
            con = drivermanager.getconnection (Url,username,password); pstm = CoN.preparestatement (SQL);
            for (int i=0;i<paras.length;i++) {pstm.setstring (i+1, paras[i]);
        rs = Pstm.executequery ();
        catch (Exception e) {e.printstacktrace ();
    }finally{//temporarily cannot close} return RS;
        }//Add, delete, and change public boolean update (String sql,string[] paras) {Boolean b = true;
        try{Class.forName (driver);
            con = drivermanager.getconnection (Url,username,password);

            pstm = con.preparestatement (sql);
            for (int i=0;i<paras.length;i++) {pstm.setstring (i+1, paras[i]);
            The///EXECUTE statement operation if (Pstm.executeupdate ()!=1) {b=false;
            The catch (Exception E1) {b=false;
        E1.printstacktrace ();
        }finally{This.close ();
    return b;
   public void Close () {try{         if (rs!=null) rs.close ();
            if (pstm!=null) pstm.close ();
        if (con!=null) con.close ();
        catch (Exception E2) {e2.printstacktrace ();
 }
    }
}

Finish

Other
Start learning to use SQLServer2008 database, introduce Sqljdbc4.jar
Oracle is practiced in the middle of hand assignment, introducing Classes12.jar,studao module for database connection modification

Create SQL Server database table student

--sqlserver
--Manually create the Database study (abbreviated)
drop table dbo.student;
CREATE TABLE Dbo.student (
    stuid  char ()  Primary key not  null,
    stuname varchar,
    stusex CHAR (2) DEFAULT ' Man ' CHECK (stusex= ' male ' OR stusex= ' female '),
    stuage varchar (3),
    studate  DateTime,
    studept varchar ()
);
ALTER TABLE student ALTER COLUMN studate Date; --Modify table column properties
insert into student values (4, ' Dick ', ' Male ', 27, ' 2016-03-28 ', ' computer system ');
SELECT * from study.dbo.student where stuname like '% Wangxiaobing% '

Problem
Error: Prompt could not find or load main class Study.student.test2.stuManage
Solution
Right-click-Debug Mode-java Application
or right-click the Stumanage interface –run As–java application

Problem
Java.lang.NullPointerException
At Study.student.test3.Model. (model.java:32)
At Study.student.test3.Model.main (model.java:70)
Reason
Missing Statement rs = Pstm.executequery ();

Problem
SQL Server loads drivers and creates database connections. Execute SQL statement
1. Load Driver
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
2. Create a connection to the database
Ct=drivermanager.getconnection ("Jdbc:sqlserver://127.0.0.1:1433;databasename=study", "sa", "123456");
3. Execute SQL statement
Pstm = Ct.preparestatement ("select * FROM [study].[ DBO]. [Student] ");
Update
rs = Pstm.executequery ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.