使用Java面向对象程序操作Oralce数据库实现界面增,删,改,查

  • 创建Oracle脚本

这里写图片描述

  • 系统界面及代码
  • 一、StuManage.java 主界面,默认显示并查询出学生
    图片描述
package StuManage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class StuManager extends JFrame implements ActionListener{
    JPanel jpanel1,jpanel2;
    JLabel jlabel1;
    JTextField jtextfield1;
    JButton jbutton1,jbutton2,jbutton3,jbutton4;
    JTable jtable1;
    JScrollPane jsp = null;
    StuModel sm = null;

    StuManager(){
        this.setLayout(new GridLayout(3,1));
        //布局一行
        jpanel1 = new JPanel();
        jlabel1 = new JLabel("用户名");
        jtextfield1 = new JTextField(10);
        jbutton1 = new JButton("查询");
        jbutton1.addActionListener(this);
        jpanel1.add(jlabel1);
        jpanel1.add(jtextfield1);
        jpanel1.add(jbutton1);

        //布局二行
        sm = new StuModel();
        String[] paras = {"1"};
        sm.queryStu("select * from student where 1=? ", paras);
        jtable1 = new JTable(sm);
        jsp = new JScrollPane(jtable1);

        //布局三行
        jpanel2  = new JPanel();
        jbutton2 = new JButton("添加");
        jbutton2.addActionListener(this);
        jbutton3 = new JButton("修改");
        jbutton3.addActionListener(this);
        jbutton4 = new JButton("删除");
        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);
    }
    //主函数
    public static void main(String[] args){
        StuManager stu = new StuManager();
        stu.setTitle("学生信息管理系统(李雁冬)");
        stu.setSize(350,300);
        stu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        stu.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //查询
        if(e.getSource()==jbutton1){
            //把对表的数据封装到stuModel中,完成查询
            String sql = "";
            String name = this.jtextfield1.getText().trim();
            if( name !=null && !name.trim().equals("")){
                sql = "select * from student where stuname= ?";
            }
            else{
                JOptionPane.showMessageDialog(jbutton1, "输入条件","提示:",1);
                return ;
            }
            //构建新的数据模型,并更新
            sm = new StuModel();
            String[] paras = {name};
            sm.queryStu(sql, paras);
            this.jtable1.setModel(sm);
        }
        //添加学生
        else if(e.getSource()==jbutton2){
            StuAdd add = new StuAdd(this,"添加学生",true);

            //*添加完成重新再获得模型,同理其他操作
            sm = new StuModel();
            String[] paras = {"1"};
            sm.queryStu("select * from student where 1 =?", paras);
            this.jtable1.setModel(sm);

        }
        //修改学生
        else if(e.getSource()==jbutton3){
            int rowNum = this.jtable1.getSelectedRow();
            System.out.println("修改第"+(rowNum+1)+"行");
            if(rowNum == -1){
            JOptionPane.showMessageDialog(this, "请选择修改的行");
                return ;
            }
            new StuUpdate(this,"修改学生",true, sm, rowNum);
            sm = new StuModel();

            String[] paras = {"1"};
            sm.queryStu("select * from student where 1 =?", paras);

            this.jtable1.setModel(sm);
        }
        //删除学生
        else if(e.getSource()==jbutton4){  
            //1.得到学生的ID,getSelectedRow会返回用户点中的行,如果一行未选,则返回-1
            int rowNum = this.jtable1.getSelectedRow();
            if(rowNum == -1){
                JOptionPane.showMessageDialog(this, "请选择所要删除的学生");
                return ;
            }

            //得到学生的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, "删除成功");
        }
    }
}
  • 二、StuModel.java *数据操作模型界面(修改、删除学生)
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;
        //表中间
        columnNames = new Vector();
        columnNames.add("学号");
        columnNames.add("姓名");
        columnNames.add("性别");
        columnNames.add("年龄");
        columnNames.add("籍贯");
        columnNames.add("所在系");
        rowData = new Vector();  //rowData可存放多行

        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));
                //加入到rowData
                rowData.add(hang);
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            he.close();
        }
    }
    @Override
    public int getRowCount() {
        // 得到列数
        return this.rowData.size();
    }
    @Override
    public int getColumnCount() {
        // 得到行数
        return this.columnNames.size();
    }
    @Override
    public Object getValueAt(int row, int column) {
        // 得到指定行、列的数据
        return ((Vector)this.rowData.get(row)).get(column);
    }
    @Override
    public String getColumnName(int column) {
        return (String) this.columnNames.get(column);
    }
}
  • 三、StuAdd.java 添加/修改学生界面
    修改时学号为置灰状态
    添加学生
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;
    //父窗口、窗口名、*是否模态
    public StuAdd(Frame owner,String title,boolean modal){
        super(owner,title,modal);
        jpanel1 = new JPanel(new GridLayout(6,1,1,1));
        jl1 = new JLabel("学号");
        jl2 = new JLabel("姓名");
        jl3 = new JLabel("性别");
        jl4 = new JLabel("年龄");
        jl5 = new JLabel("籍贯");
        jl6 = new JLabel("所在系");
        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("保存");
        jbutton1.addActionListener(this);
        jbutton2 = new JButton("取消");
        jpanel3.add(jbutton1);
        jpanel3.add(jbutton2);

        this.add(jpanel1,BorderLayout.WEST);
        this.add(jpanel2,BorderLayout.EAST);
        this.add(jpanel3,BorderLayout.SOUTH);
        this.setTitle("添加学生信息");
        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, "添加失败");
            }
            else{
                this.setVisible(false);
                JOptionPane.showMessageDialog(this, "添加成功");
            }

            //关闭对话框
            this.dispose();

        }
    }
}
  • 四、StuDao.java //操作Oracle数据库实体类,进行增、删、改操作合并
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{
            //暂时不能关闭
        }
        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{
            //暂时不能关闭
        }
        return rs;
    }
    //增、删、改合并
    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]);
            }

            //执行语句操作
            if(pstm.executeUpdate()!=1){
                b=false;
            }
        }
        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();
        }
    }
}

  • 其他
    开始学习用SQLServer2008数据库,引入sqljdbc4.jar
    交作业中间练习了Oracle,引入classes12.jar,stuDAO模块进行数据库的连接修改

  • 创建SQLServer数据库表student

--sqlserver
--手动创建数据库study(略)
drop table dbo.student;
create table dbo.student(
    stuID  char(10)  Primary key  not null,
    stuName varchar(20),
    stuSex CHAR(2) DEFAULT '男' CHECK(StuSex='男' OR stuSex='女'),
    stuAge varchar(3),
    stuDate  DateTime,
    stuDept varchar(10)
);
ALTER TABLE student ALTER column stuDate Date; --修改表列属性
insert into student values(4,'李四','男',27,'2016-03-28','计算机系');
select * from study.dbo.student where stuName like '%王小兵%'

【问题】
错误: 提示找不到或无法加载主类 study.student.test2.stuManage
[解决]
右击-调试方式-java应用程序
或右击stuManage界面–Run As–Java Application


【问题】
java.lang.NullPointerException
at study.student.test3.Model.(Model.java:32)
at study.student.test3.Model.main(Model.java:70)
[原因]
缺少语句 rs = pstm.executeQuery();


【问题】
SQLServer加载驱动、创建数据库连接。执行SQL语句
//1.加载驱动
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
//2.创建数据库的连接
ct=DriverManager.getConnection(“jdbc:sqlserver://127.0.0.1:1433;DatabaseName=study”,”sa”,”123456”);
//3.执行SQL语句
pstm = ct.prepareStatement(“select * from [study].[dbo].[student]”);
//更新
rs = pstm.executeQuery();

展开阅读全文

没有更多推荐了,返回首页