• Java基础8


    Java基础8    1

    文件结构    1

    使用jdbc-odbc桥连_Class18/ com.test1/test1.java    3

    PreparedStatement的使用_Class18/ com.test1/test2.java    5

    jdbc方式操作数据库操作数据库_Class18/ com.test2/test1.java    7

    java中使用ddl语句_Class18/ com.test1/test2.java    9

    JTable使用_Class19/ com.test1/test1.java    11

    从数据库中取出学生信息_Class19/ com.test1/test2.java    12

    Min版学生管理系统_数据库设计    14

    min版学生管理系统1.0_Class19/ com.test1/test3.java    15

    min版学生管理系统1.0_Class19/ com.test1/ StuAddDialog.java    18

    min版学生管理系统1.0_Class19/ com.test1/ StuModel.java    21

    min版学生管理系统2.0_Class20/ com.test1/ StuManage.java    24

    min版学生管理系统2.0_Class20/ com.test1/ StuAddDialog.java    28

    min版学生管理系统2.0_Class20/ com.test1/ StuUpdateDialog.java    31

    min版学生管理系统2.0_Class20/ com.test1/ StuModel.java    35

    min版学生管理系统3.0_Class20/ com.test2/ StuManage.java    38

    min版学生管理系统3.0_Class20/ com.test2/ StuAddDialog.java    42

    min版学生管理系统3.0_Class20/ com.test2/ StuUpdateDialog.java    45

    min版学生管理系统3.0_Class20/ com.test2/ StuModel.java    48

    min版学生管理系统3.0_Class20/ com.test2/ SqlHelper.java    50

    文件结构

    使用jdbc-odbc桥连_Class18/ com.test1/test1.java

    /*

    * 演示使用jdbc-odbc桥连方式操作数据库

    * 1.配置数据源

    * 2.在程序中去连接数据源

    */

    package com.test1;

    import java.sql.*;

    public class Test1 {

        

        public static void main(String[] args) {

            Connection ct=null;    //定义一个Connection

            Statement sm=null;    //定义一个Statement

            try {

                //1.加载驱动(把需要的驱动程序加入内存)

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                

                //2.得到连接[指定连接到哪个数据源,用户名和密码]

                //如果配置数据源时,选择的是windows nt验证,则不需要加"sa","luowei"

                //即:Connection ct=DriverMannager.getConnection("jdbc:odbc:mytest");

                ct=DriverManager.getConnection("jdbc:odbc:mytest","sa","luowei");

                

                //3.创建Statement或者PreparedStatement[区别]

                //Statement用处是:主要用于发送sql语句到数据库

                sm=ct.createStatement();

                

                //4.执行(crud,创建数据库,备份数据库,删除数据库....)

                    //1.演示添加一条数据到dept

                //executeUpdate可以执行cud操作(添加,删除,修改)

                int i=sm.executeUpdate("insert into dept values('50','保安部','北京')");

                if(i==1)

                {

                    System.out.println("添加ok");

                }

                else

                {

                    System.out.println("添加error");

                }

                    //2.修改从dept表记录

                int k=sm.executeUpdate("update dept set loc='北京 ' where deptno='40'");

                if(k==1)

                {

                    System.out.println("更新成功");

                }

                else

                {

                    System.out.println("更新不成功");

                }

                    //3.演示从dept表中删除一条记录

                int j=sm.executeUpdate("delete from dept where deptno='50'");

                if(j==1)

                {

                    System.out.println("删除ok");

                }

                else

                {

                    System.out.println("删除error");

                }

                //查询,显示所有部门

                //ResultSet结果集

                ResultSet rs=sm.executeQuery("select * from dept;");

                //rs指向结果集的第一行的前一行

                //循环取出

                while(rs.next())

                {

                    int deptno=rs.getInt(1);

                    String dname=rs.getString(2);

                    String loc=rs.getString(3);

                    System.out.println(deptno+" "+dname+" "+loc);

                }

    //            //试图取出1行的第一列

    //            rs.next();

    //            int a=rs.getInt(1);//取出第1

    //            System.out.println(a);

    //            //取出第一行第2

    //            String b=rs.getString(2);

    //            System.out.println(b);

                

                //5.关闭资源,关闭顺序是,谁后创建,则谁先关闭

                try {

                    if(sm!=null)

                    {

                        rs.close();

                        sm.close();

                        ct.close();

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

    PreparedStatement的使用_Class18/ com.test1/test2.java

    /*

    * PreparedStatement的使用

    * 1.PreparedStatement可以提高执行效率(因为它有预编译的功能)

    * 2.PreparedStatement可以防止sql注入,但是要求用?赋值的方式才可以

    */

    package com.test1;

    import java.sql.*;

    public class Test2 {

        

        public static void main(String[] args) {

            Connection ct=null;    //定义一个Connection

            PreparedStatement ps=null;    //定义一个Statement

            ResultSet rs=null;

            try {

                //1.加载驱动(把需要的驱动程序加入内存)

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                

                //2.得到连接[指定连接到哪个数据源,用户名和密码]

                //如果配置数据源时,选择的是windows nt验证,则不需要加"sa","luowei"

                //即:Connection ct=DriverMannager.getConnection("jdbc:odbc:mytest");

                ct=DriverManager.getConnection("jdbc:odbc:mytest","sa","luowei");

                

                //3.创建PreparedStatement - ps

                //Statement用处是:主要用于发送sql语句到数据库

                ps=ct.prepareStatement("select * from dept where deptno=? and loc=?");

                //给?赋值,用?赋值可以防止注入漏洞

                ps.setInt(1, 20);

                ps.setString(2, "dallas");

                rs=ps.executeQuery();

                

                //4.查询

                //ResultSet结果集

                //循环取出

                while(rs.next())

                {

                    int deptno=rs.getInt(1);

                    String dname=rs.getString(2);

                    String loc=rs.getString(3);

                    System.out.println(deptno+" "+dname+" "+loc);

                }

                

                //使用PrepareStatement添加一条记录

                ps=ct.prepareStatement("insert into dept values(?,?,?)");

                ps.setInt(1, 50);

                ps.setString(2,"安全部");

                ps.setString(3, "北京");

                int i=ps.executeUpdate();

                if(i==1)

                {

                    System.out.println("添加ok");

                }

                else

                {

                    System.out.println("添加error");

                }

                

                //5.关闭资源,关闭顺序是,谁后创建,则谁先关闭

                try {

                    if(rs!=null)

                    {

                        rs.close();

                    }

                    if(ps!=null)

                    {

                        ps.close();

                    }

                    if(ps!=null)

                    {

                        ct.close();

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

    jdbc方式操作数据库操作数据库_Class18/ com.test2/test1.java

    /*

    * jdbc方式操作数据库

    * 1.java.sql.*;引入

    * 2.需要引入三个jar

    * 3.如果取值按编号,则需要一一对应,如果是按名字取的,则顺序可以倒乱

    */

    package com.test2;

    import java.sql.*;

    public class Test1 {

        public static void main(String[] args) {

            //PreparedStatement[火箭车]

            //定义需要的对象

            PreparedStatement ps=null;

            Connection ct=null;

            ResultSet rs=null;

            

            try {

                //初始化我们的对象

                //1.加载驱动

                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                //2.得到连接

                ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://" +

                        "127.0.0.1:1433;databaseName=LiangshanHeros2","sa","luowei");

                //3.创建火箭车

                ps=ct.prepareStatement("select ename,sal,dname from emp,dept " +

                        "where emp.deptno=dept.deptno");

                //4.执行[如果是增加,删除,修改 使用executeUpdate(),如果是查询使用executeQuery()]

                rs=ps.executeQuery();

                //循环取出,雇员的名字,雇员的薪水,部门名称

                while(rs.next())

                {

                    //String name=rs.getString(1);

                    //float sal=rs.getFloat(2);

                    //int deptno=rs.getInt(3);

                    String name=rs.getString("ename");

                    float sal=rs.getFloat("sal");

                    String deptno=rs.getString("dname");

                    System.out.println(name+" "+sal+" "+deptno);

                }

                //添加,删除,修改

                ps=ct.prepareStatement("insert into dept values(?,?,?)");

                ps.setInt(1, 100);

                ps.setString(2, "财务部");

                ps.setString(3, "大学城");

                

                //执行[如果是增加,删除,修改 使用executeUpdate(),如果是查询使用executeQuery()]

                int i=ps.executeUpdate();

                if(i==1)

                {

                    System.out.println("添加ok");

                }

                else

                {

                    System.out.println("添加error");

                }

                

                //5.关闭资源,关闭顺序是,谁后创建,则谁先关闭

                try {

                    if(rs!=null)

                    {

                        rs.close();

                    }

                    if(ps!=null)

                    {

                        ps.close();

                    }

                    if(ps!=null)

                    {

                        ct.close();

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

    java中使用ddl语句_Class18/ com.test1/test2.java

    /*

    * java中使用ddl语句(create,drop,backup...)

    */

    package com.test2;

    import java.sql.*;

    public class Test2 {

        

        public static void main(String[] args) {

            //定义需要的对象

            PreparedStatement ps=null;

            Connection ct=null;

            ResultSet rs=null;

            

            try {

                //加载驱动

                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                //得到连接

                ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;" +

                        "databaseName=LiangshanHeros2","sa","luowei");

                //创建ps,创建数据库,创建表,备份数据库

                ps=ct.prepareStatement("create database vvv");

                ps=ct.prepareStatement("create table xxx(aa int)");

                ps=ct.prepareStatement("backup database aaa to disk='f:/123.bak'");

                //如果执行的是ddl语句

                boolean b=ps.execute();

                System.out.println("OK "+b);

                

                try {

                    if(rs!=null)

                    {

                        rs.close();

                    }

                    if(ps!=null)

                    {

                        ps.close();

                    }

                    if(ps!=null)

                    {

                        ct.close();

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

    JTable使用_Class19/ com.test1/test1.java

    /*

    * JTable使用

    */

    package com.test1;

    import javax.swing.*;

    import java.awt.*;

    import java.sql.*;

    import java.util.*;

    import java.awt.event.*;

    public class Test1 extends JFrame{

        //rowData用来存放行数据

        //columnNames存放列名

        Vector rowData,columnNames;

        JTable jt=null;

        JScrollPane jsp=null;

        

        public static void main(String[] args) {

            Test1 test1=new Test1();

        }

        //构造函数

        public Test1()

        {

            Vector columnNames=new Vector();

            //设置列名

            columnNames.add("学号");

            columnNames.add("名字");

            columnNames.add("性别");

            columnNames.add("年龄");

            columnNames.add("籍贯");

            columnNames.add("系别");

            

            rowData=new Vector();

            //rowData可以存放多行

            Vector hang=new Vector();

            hang.add("lw001");

            hang.add("孙悟空");

            hang.add("");

            hang.add("500");

            hang.add("花果山");

            hang.add("少林派");

            

            //加入到rowData

            rowData.add(hang);

            

            //初始化JTable

            jt=new JTable(rowData,columnNames);

            

            //初始化jsp JScrollPane

            jsp=new JScrollPane(jt);

            

            //jsp放入到jframe

            this.add(jsp);

            this.setSize(400,300);

            

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setVisible(true);

        }

    }

    从数据库中取出学生信息_Class19/ com.test1/test2.java

    /*

    * 从数据库中取出学生信息

    */

    package com.test1;

    import java.util.Vector;

    import javax.swing.JFrame;

    import javax.swing.JScrollPane;

    import javax.swing.JTable;

    import java.sql.*;

    public class Test2 extends JFrame{

        //rowData用来存放行数据

        //columnNames存放列名

        Vector rowData,columnNames;

        JTable jt=null;

        JScrollPane jsp=null;

        

        //定义操作数据库需要的东西

        PreparedStatement ps=null;

        Connection ct=null;

        ResultSet rs=null;

          

        

        public static void main(String[] args) {

            Test2 test2=new Test2();

        }

        public Test2()

        {

            columnNames=new Vector();

            //设置列名

            columnNames.add("学号");

            columnNames.add("名字");

            columnNames.add("性别");

            columnNames.add("年龄");

            columnNames.add("籍贯");

            columnNames.add("系别");

            

            rowData=new Vector();

            try {

                //1.加载驱动

                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;" +

                        "databaseName=LiangShanHeros","sa","luowei");

                ps=ct.prepareStatement("select * from stu");

                rs=ps.executeQuery();

                

                while(rs.next())

                {

                    //rowData可以存放多行

                    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();

                    if(ps!=null) ps.close();

                    if(ct!=null) ct.close();

                    

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

              

            

            //初始化JTable

            jt=new JTable(rowData,columnNames);

            

            //初始化jsp JScrollPane

            jsp=new JScrollPane(jt);

            

            //jsp放入到jframe

            this.add(jsp);

            this.setSize(400,300);

            

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setVisible(true);

        }

    }

    Min版学生管理系统_数据库设计

    --创建学生表

     

    create table stu(

    stuId varchar(30) primary key,

    stuName nvarchar(50) not null,

    stuSex nchar(1) check(stuSex in('男','女')) default '男',

    stuAge int check(stuAge>1),

    stuJg nvarchar(30),

    stuDept nvarchar(40)

    )

     

    insert into stu values('lw001','孙悟空','男',20,'花果山','少林派');

    insert into stu values('lw002','猪八戒','男',15,'高老庄','天上的');

    insert into stu values('lw003','沙悟净','男',26,'流沙河','水里的');

    insert into stu values('lw004','唐三藏','男',21,'长安','庙里的');

     

    select * from stu;

    min版学生管理系统1.0_Class19/ com.test1/test3.java

    /*

    * 完成一个min版本的学生管理系统

    */

    package com.test1;

    import java.awt.*;

    import java.awt.event.*;

    import java.util.*;

    import javax.swing.*;

    import com.test1.StuModel;

    import java.sql.*;

    public class Test3 extends JFrame implements ActionListener{

        //定义一些控件

        JPanel jp1,jp2;

        JLabel jl1;

        JButton jb1,jb2,jb3,jb4;

        JTable jt;

        JScrollPane jsp;

        JTextField jtf;

          

          

          

        

        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

            jp1.add(jl1);

            jp1.add(jtf);

            jp1.add(jb1);

            

            jp2=new JPanel();

            

            jb2=new JButton("添加");

            jb2.addActionListener(this);

            jb3=new JButton("修改");

            jb4=new JButton("请输入名字");

            

            //把各个按钮加入到jp2

            jp2.add(jb2);

            jp2.add(jb3);

            jp2.add(jb4);

            

            //中间

            

            //创建一个数据模型对象

            StuModel sm=new StuModel();

            

            //初始化JTable

            jt=new JTable(sm);

            

            //初始化jsp JScrollPane

            jsp=new JScrollPane(jt);

            

            //jsp放入到jframe

            this.add(jsp);

            this.add(jp1,"North");

            this.add(jp2,"South");

            

            this.setSize(400,300);

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setVisible(true);

            

        }

        @Override

        public void actionPerformed(ActionEvent e) {

            //判断是哪个按钮被点击

            if(e.getSource()==jb1)

            {

                System.out.println("用户想查询!");

                //...

                //查询数据库,更新JTable,这样大量的代码重复,解决办法:

                //把对表操作封装成一个类,可以完成对表的操作

                //把对表的数据装到StuModel中,就可以比较简单的完成查询

                String name=this.jtf.getText().trim();

                //写一个sql语句

                String sql="select * from stu where stuname='"+name+"'";

                //构建新的数据模型类,并更新

                StuModel sm=new StuModel(sql);

                //更新JTable

                jt.setModel(sm);

            }

            //当用户点击添加

            else if(e.getSource()==jb2)

            {

                StuAddDialog sa=new StuAddDialog(this,"添加学生",true);

                

                //重新再获得新的数据模型

            }

        }

    }

    min版学生管理系统1.0_Class19/ com.test1/ StuAddDialog.java

    package com.test1;

    import java.awt.BorderLayout;

    import java.awt.Frame;

    import java.awt.GridLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.beans.Statement;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import javax.swing.JButton;

    import javax.swing.JDialog;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JPanel;

    import javax.swing.JTextField;

    public class StuAddDialog extends JDialog implements ActionListener{

        //定义需要的控件

        JLabel jl1,jl2,jl3,jl4,jl5;

        JButton jb1,jb2;

        JTextField jtf1,jtf2,jtf3,jtf4,jtf5;

        JPanel jp1,jp2,jp3;

        

        //owner它的父窗口

        //title窗口名

        //modal指定模式窗口,还是非模式窗口

        public StuAddDialog(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("记过次数");

            

            jtf1=new JTextField();

            jtf2=new JTextField();

            jtf3=new JTextField();

            jtf4=new JTextField();

            jtf5=new JTextField();

            

            jb1=new JButton("添加");

            jb2=new JButton("取消");

            

            jp1=new JPanel();

            jp2=new JPanel();

            jp3=new JPanel();

            

            //设置布局

            jp1.setLayout(new GridLayout(5,1));

            jp2.setLayout(new GridLayout(5,1));

            

            //添加组件

            jp1.add(jl1);

            jp1.add(jl2);

            jp1.add(jl3);

            jp1.add(jl4);

            jp1.add(jl5);

            

            jp2.add(jtf1);

            jp2.add(jtf2);

            jp2.add(jtf3);

            jp2.add(jtf4);

            jp2.add(jtf5);

            

            jp3.add(jb1);

            jp3.add(jb2);

            

            this.add(jp1,BorderLayout.WEST);

            this.add(jp2,BorderLayout.CENTER);

            this.add(jp3,BorderLayout.SOUTH);

            

            jb1.addActionListener(this);

            

            //展现对话框

            this.setSize(300,200);

            //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setVisible(true);

        }

        @Override

        public void actionPerformed(ActionEvent e) {

            //连接数据库

            Connection conn=null;

            //Statement stmt=null;

            ResultSet rs=null;

            PreparedStatement pstmt=null;

            //连接数据库,判断用户是否合法

            try {

                //1.加载驱动

                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                //2.创建连接对象

                String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;" +

                        "databaseName=LiangShanHeros";

                //2..注册驱动程序

                conn=DriverManager.getConnection(url,"sa","luowei");

                //3.获取语句对象

                //stmt=conn.createStatement();

                

                //编译的语句对象

                String strsql="insert into student values(?,?,?,?,?)";

                pstmt=conn.prepareStatement(strsql);

                

                //给参数赋值

                pstmt.setString(1,jtf1.getText());

                pstmt.setString(2,jtf2.getText());

                pstmt.setString(3, jtf3.getText());

                pstmt.setString(4, jtf4.getText());

                pstmt.setString(5, jtf5.getText());

                

                //4.执行操作

                pstmt.executeUpdate();

                

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

            } catch (Exception e2) {

                e2.printStackTrace();

            }finally{

                //5释放语句对象,连接对象

                try {

                    if(rs!=null) rs.close();

                    if(pstmt!=null) pstmt.close();

                    if(conn!=null) conn.close();

                } catch (Exception e3) {

                    e3.printStackTrace();

                }

            }

        }

    }

    min版学生管理系统1.0_Class19/ com.test1/ StuModel.java

    /*

    * 这是一个stu表的模型

    * 可以把对stu表的各种操作封装到该模型中

    */

    package com.test1;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.util.Vector;

    import javax.swing.table.*;

    public class StuModel extends AbstractTableModel{

        //rowData用来存放行数据

        //columnNames存放列名

        Vector rowData,columnNames;

        

        //定义操作数据库需要的东西

        PreparedStatement ps=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=new Vector();

            try {

                //1.加载驱动

                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;" +

                        "databaseName=LiangShanHeros","sa","luowei");

                ps=ct.prepareStatement(sql);

                rs=ps.executeQuery();

                

                while(rs.next())

                {

                    //rowData可以存放多行

                    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();

                    if(ps!=null) ps.close();

                    if(ct!=null) ct.close();

                    

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        }

        

        public void addStu(String sql)

        {

            //根据用户输入的sql语句,完成添加...

        }

        

        //通过传递的sqL语句来获得数据模型

        public StuModel(String sql)

        {

            this.init(sql);

        }

        

        //做一个构造函数,用于初始化我们的数据模型

        public StuModel()

        {

            this.init("");

        }

        

        @Override //得到共有多少列

        public int getColumnCount() {

            //System.out.println("getColumncount");

            return this.columnNames.size();

        }

        @Override //得到共有多少行

        public int getRowCount() {

            //

            return this.rowData.size();

        }

        @Override //得到某行某列的数据

        public Object getValueAt(int rowIndex, int columnIndex) {

            //return null;

            return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);

        }

        @Override

        public String getColumnName(int column) {

            // 重写这个方法,修改默认的列名

            return (String)this.columnNames.get(column);

        }

    }

    min版学生管理系统2.0_Class20/ com.test1/ StuManage.java

    /*

    * 完成一个min版本的学生管理系统

    * 1.查询任务

    * 2.添加一个学生

    */

    package com.test1;

    import java.awt.*;

    import java.awt.event.*;

    import java.util.*;

    import javax.swing.*;

    import com.test1.StuModel;

    import java.sql.*;

    public class StuManage extends JFrame implements ActionListener{

        //定义一些控件

        JPanel jp1,jp2;

        JLabel jl1;

        JButton jb1,jb2,jb3,jb4;

        JTable jt;

        JScrollPane jsp;

        JTextField jtf;

        StuModel sm;

        

        //定义操作数据库需要的东西

        PreparedStatement ps=null;

        Connection ct=null;

        ResultSet rs=null;

          

          

        

        public static void main(String[] args) {

            StuManage test3=new StuManage();

        }

        //构造函数

        public StuManage()

        {

            jp1=new JPanel();

            jtf=new JTextField(10);

            jb1=new JButton("查询");

            jb1.addActionListener(this);//添加监听

            

            jl1=new JLabel("请输入名字:");

            

            //把各个控件加入jp1

            jp1.add(jl1);

            jp1.add(jtf);

            jp1.add(jb1);

            

            jp2=new JPanel();

            

            jb2=new JButton("添加");

            jb2.addActionListener(this);

            jb3=new JButton("修改");

            jb3.addActionListener(this);

            jb4=new JButton("删除");

            jb4.addActionListener(this);

            

            //把各个按钮加入到jp2

            jp2.add(jb2);

            jp2.add(jb3);

            jp2.add(jb4);

            

            //中间

            

            //创建一个数据模型对象

            sm=new StuModel();

            

            //初始化JTable

            jt=new JTable(sm);

            

            //初始化jsp JScrollPane

            jsp=new JScrollPane(jt);

            

            //jsp放入到jframe

            this.add(jsp);

            this.add(jp1,"North");

            this.add(jp2,"South");

            

            this.setSize(400,300);

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setVisible(true);

            

        }

        @Override

        public void actionPerformed(ActionEvent e) {

            //判断是哪个按钮被点击

            if(e.getSource()==jb1)

            {

                //对用户点击添加按钮的响应动作

                System.out.println("用户想查询!");

                //...

                //查询数据库,更新JTable,这样大量的代码重复,解决办法:

                //把对表操作封装成一个类,可以完成对表的操作

                //把对表的数据装到StuModel中,就可以比较简单的完成查询

                String name=this.jtf.getText().trim();

                //写一个sql语句

                String sql="select * from stu where stuName='"+name+"'";

                //构建新的数据模型类,并更新

                sm=new StuModel(sql);

                //更新JTable

                jt.setModel(sm);

            }

            //当用户点击添加

            else if(e.getSource()==jb2)

            {

                StuAddDialog sa=new StuAddDialog(this,"添加学生",true);

                

                //重新再获得新的数据模型

                sm=new StuModel();

                //更新JTable

                jt.setModel(sm);

            }

            else if(e.getSource()==jb3)

            {

                //用户希望修改

                int rowNum=this.jt.getSelectedRow();

                if(rowNum==-1)

                {

                    //提示

                    JOptionPane.showMessageDialog(this, "请选择一行");

                    return ;

                }

                //显示修改对话框

                new StuUpdateDialog(this,"修改学生",true,sm,rowNum);

                

                //重新再获得新的数据模型

                sm=new StuModel();

                //更新JTable

                jt.setModel(sm);

            }

            

            else if(e.getSource()==jb4)

            {

                //说明用户希望删除记录

                //1.得到该学生的id

                int rowNum=this.jt.getSelectedRow();//返回用户点中的行,如果没有选返返-1

                if(rowNum==-1)

                {

                    //提示

                    JOptionPane.showMessageDialog(this,"请选择一行");

                    return ;

                }

                //得到学生编号

                String stuId=(String)sm.getValueAt(rowNum, 0);

                //System.out.println("id="+stuId);

                //连接数据库,完成删除任务

                try {

                    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                    ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;" +

                            "databaseName=LiangshanHeros","sa","luowei");

                    ps=ct.prepareStatement("delete from stu where stuid=?");

                    ps.setString(1, stuId);

                    ps.executeUpdate();

                } catch (Exception e2) {

                    e2.printStackTrace();

                }finally{

                    //关闭资源

                    try {

                        if(rs!=null) rs.close();

                        if(ps!=null) ps.close();

                        if(ct!=null) ct.close();

                    } catch (Exception e3)

                    {

                        e3.printStackTrace();

                    }

                }

                //更新数据模型

                sm=new StuModel();

                //更新JTable

                jt.setModel(sm);

            }

        }

    }

    min版学生管理系统2.0_Class20/ com.test1/ StuAddDialog.java

    package com.test1;

    import java.awt.BorderLayout;

    import java.awt.Frame;

    import java.awt.GridLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.beans.Statement;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import javax.swing.JButton;

    import javax.swing.JDialog;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JPanel;

    import javax.swing.JTextField;

    public class StuAddDialog extends JDialog implements ActionListener{

        //定义需要的控件

        JLabel jl1,jl2,jl3,jl4,jl5,jl6;

        JButton jb1,jb2;

        JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;

        JPanel jp1,jp2,jp3;

        

        //owner它的父窗口

        //title口名

        //modal指定模式窗口,还是非模式窗口

        public StuAddDialog(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("系别");

            

            jtf1=new JTextField();

            jtf2=new JTextField();

            jtf3=new JTextField();

            jtf4=new JTextField();

            jtf5=new JTextField();

            jtf6=new JTextField();

            

            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));

            

            //添加组件

            jp1.add(jl1);

            jp1.add(jl2);

            jp1.add(jl3);

            jp1.add(jl4);

            jp1.add(jl5);

            jp1.add(jl6);

            

            jp2.add(jtf1);

            jp2.add(jtf2);

            jp2.add(jtf3);

            jp2.add(jtf4);

            jp2.add(jtf5);

            jp2.add(jtf6);

            

            jp3.add(jb1);

            jp3.add(jb2);

            

            this.add(jp1,BorderLayout.WEST);

            this.add(jp2,BorderLayout.CENTER);

            this.add(jp3,BorderLayout.SOUTH);

              

            

            //展现对话框

            this.setSize(300,250);

            //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setTitle("添加学生");

            this.setVisible(true);

        }

        @Override

        public void actionPerformed(ActionEvent e) {

            if(e.getSource()==jb1)

            {

                //连接数据库

                Connection conn=null;

                //Statement stmt=null;

                ResultSet rs=null;

                PreparedStatement pstmt=null;

                //连接数据库,判断用户是否合法

                try {

                    //1.加载驱动

                    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                    //2.创建连接对象

                    String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;" +

                            "databaseName=LiangShanHeros";

                    //2..注册驱动程序

                    conn=DriverManager.getConnection(url,"sa","luowei");

                    //3.获取语句对象

                    //stmt=conn.createStatement();

                    

                    //预编译的语句对象

                    String strsql="insert into stu values(?,?,?,?,?,?)";

                    pstmt=conn.prepareStatement(strsql);

                    

                    //给参数赋值

                    pstmt.setString(1,jtf1.getText());

                    pstmt.setString(2,jtf2.getText());

                    pstmt.setString(3,jtf3.getText());

                    pstmt.setString(4,jtf4.getText());

                    pstmt.setString(5,jtf5.getText());

                    pstmt.setString(6,jtf6.getText());

                    

                    //4.执行操作

                    pstmt.executeUpdate();

                    

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

                } catch (Exception e2) {

                    e2.printStackTrace();

                }finally{

                    //5释放语句对象,连接对象

                    try {

                        if(rs!=null) rs.close();

                        if(pstmt!=null) pstmt.close();

                        if(conn!=null) conn.close();

                    } catch (Exception e3) {

                        e3.printStackTrace();

                    }

                }

            }

        }

    }

    min版学生管理系统2.0_Class20/ com.test1/ StuUpdateDialog.java

    /*

    * 修改学生

    */

    package com.test1;

    import java.awt.BorderLayout;

    import java.awt.Frame;

    import java.awt.GridLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.beans.Statement;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import javax.swing.JButton;

    import javax.swing.JDialog;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JPanel;

    import javax.swing.JTextField;

    public class StuUpdateDialog extends JDialog implements ActionListener{

        //定义需要的控件

        JLabel jl1,jl2,jl3,jl4,jl5,jl6;

        JButton jb1,jb2;

        JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;

        JPanel jp1,jp2,jp3;

        

        //owner它的父窗口

        //title窗口名

        //modal指定模式窗口,还是非模式窗口

        public StuUpdateDialog(Frame owner,String title,boolean modal,StuModel sm,int rowNums)

        {

            super(owner,title,modal);//调用父类构造方法,达到模式对话框效果

            jl1=new JLabel("学号");

            jl2=new JLabel("姓名");

            jl3=new JLabel("性别");

            jl4=new JLabel("年龄");

            jl5=new JLabel("籍贯");

            jl6=new JLabel("系别");

            

            jtf1=new JTextField();

            //初始化数据

            jtf1.setText((String)sm.getValueAt(rowNums, 0));

            //jtf1,不能修改

            jtf1.setEditable(false);

            jtf2=new JTextField();

            jtf2.setText((String)sm.getValueAt(rowNums, 1));

            jtf3=new JTextField();

            jtf3.setText((String)sm.getValueAt(rowNums, 2));

            jtf4=new JTextField();

            jtf4.setText(sm.getValueAt(rowNums, 3).toString());

            jtf5=new JTextField();

            jtf5.setText((String)sm.getValueAt(rowNums, 4));

            jtf6=new JTextField();

            jtf6.setText((String)sm.getValueAt(rowNums, 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));

            

            //添加组件

            jp1.add(jl1);

            jp1.add(jl2);

            jp1.add(jl3);

            jp1.add(jl4);

            jp1.add(jl5);

            jp1.add(jl6);

            

            jp2.add(jtf1);

            jp2.add(jtf2);

            jp2.add(jtf3);

            jp2.add(jtf4);

            jp2.add(jtf5);

            jp2.add(jtf6);

            

            jp3.add(jb1);

            jp3.add(jb2);

            

            this.add(jp1,BorderLayout.WEST);

            this.add(jp2,BorderLayout.CENTER);

            this.add(jp3,BorderLayout.SOUTH);

              

            

            //展现对话框

            this.setSize(300,250);

            //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setTitle("修改学生");

            this.setVisible(true);

        }

        @Override

        public void actionPerformed(ActionEvent e) {

            if(e.getSource()==jb1)

            {

                //连接数据库

                Connection conn=null;

                //Statement stmt=null;

                ResultSet rs=null;

                PreparedStatement pstmt=null;

                //连接数据库,判断用户是否合法

                try {

                    //1.加载驱动

                    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                    //2.创建连接对象

                    String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;" +

                            "databaseName=LiangShanHeros";

                    //2..注册驱动程序

                    conn=DriverManager.getConnection(url,"sa","luowei");

                    //3.获取语句对象

                    //stmt=conn.createStatement();

                    

                    //预编译的语句对象

                    String strsql="update stu set stuName=?,stuSex=?," +

                            "stuAge=?,stuJg=?,stuDept=? where stuId=?";

                    

                    pstmt=conn.prepareStatement(strsql);

                    

                    //给参数赋值

                    pstmt.setString(1,jtf2.getText());

                    pstmt.setString(2,jtf3.getText());

                    pstmt.setString(3,jtf4.getText());

                    pstmt.setString(4,jtf5.getText());

                    pstmt.setString(5,jtf6.getText());

                    pstmt.setString(6,jtf1.getText());

                    

                    //4.执行操作

                    pstmt.executeUpdate();

                    

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

                } catch (Exception e2) {

                    e2.printStackTrace();

                }finally{

                    //5释放语句对象,连接对象

                    try {

                        if(rs!=null) rs.close();

                        if(pstmt!=null) pstmt.close();

                        if(conn!=null) conn.close();

                    } catch (Exception e3)

                    {

                        e3.printStackTrace();

                    }

                }

            }

        }

    }

    min版学生管理系统2.0_Class20/ com.test1/ StuModel.java

    /*

    * 这是一个stu表的模型

    * 可以把对stu表的各种操作封装到该模型中

    */

    package com.test1;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.util.Vector;

    import javax.swing.table.*;

    public class StuModel extends AbstractTableModel{

        //rowData用来存放行数据

        //columnNames存放列名

        Vector rowData,columnNames;

        

        //定义操作数据库需要的东西

        PreparedStatement ps=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=new Vector();

            try {

                //1.加载驱动

                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;" +

                        "databaseName=LiangShanHeros","sa","luowei");

                ps=ct.prepareStatement(sql);

                rs=ps.executeQuery();

                

                while(rs.next())

                {

                    //rowData可以存放多行

                    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();

                    if(ps!=null) ps.close();

                    if(ct!=null) ct.close();

                    

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        }

        

        public void addStu(String sql)

        {

            //根据用户输入的sql语句,完成添加...

        }

        

        //通过传递的sqL语句来获得数据模型

        public StuModel(String sql)

        {

            this.init(sql);

        }

        

        //做一个构造函数,用于初始化我们的数据模型

        public StuModel()

        {

            this.init("");

        }

        

        @Override //得到共有多少列

        public int getColumnCount() {

            //System.out.println("getColumncount");

            return this.columnNames.size();

        }

        @Override //得到共有多少行

        public int getRowCount() {

            //

            return this.rowData.size();

        }

        @Override //得到某行某列的数据

        public Object getValueAt(int rowIndex, int columnIndex) {

            //return null;

            return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);

        }

        @Override

        public String getColumnName(int column) {

            // 重写这个方法,修改默认的列名

            return (String)this.columnNames.get(column);

        }

    }

    min版学生管理系统3.0_Class20/ com.test2/ StuManage.java

    /*

    * 完成一个min版本的学生管理系统model2

    * 1.查询任务

    * 2.添加一个学生

    */

    package com.test2;

    import java.awt.*;

    import java.awt.event.*;

    import java.util.*;

    import javax.swing.*;

    import java.sql.*;

    public class StuManage extends JFrame implements ActionListener{

        //定义一些控件

        JPanel jp1,jp2;

        JLabel jl1;

        JButton jb1,jb2,jb3,jb4;

        JTable jt;

        JScrollPane jsp;

        JTextField jtf;

        StuModel sm;

          

          

          

        

        public static void main(String[] args) {

            StuManage test3=new StuManage();

        }

        //构造函数

        public StuManage()

        {

            jp1=new JPanel();

            jtf=new JTextField(10);

            jb1=new JButton("查询");

            jb1.addActionListener(this);//添加监听

            

            jl1=new JLabel("请输入名字:");

            

            //把各个控件加入jp1

            jp1.add(jl1);

            jp1.add(jtf);

            jp1.add(jb1);

            

            jp2=new JPanel();

            

            jb2=new JButton("添加");

            jb2.addActionListener(this);

            jb3=new JButton("修改");

            jb3.addActionListener(this);

            jb4=new JButton("删除");

            jb4.addActionListener(this);

            

            //把各个按钮加入到jp2

            jp2.add(jb2);

            jp2.add(jb3);

            jp2.add(jb4);

            

            //中间

            

            //创建一个数据模型对象

            sm=new StuModel();

            String []paras={"1"};

            sm.queryStu("select * from stu where 1=?", paras);

            

            //初始化JTable

            jt=new JTable(sm);

            

            //初始化jsp JScrollPane

            jsp=new JScrollPane(jt);

            

            //jsp放入到jframe

            this.add(jsp);

            this.add(jp1,"North");

            this.add(jp2,"South");

            

            this.setSize(400,300);

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setVisible(true);

            

        }

        @Override

        public void actionPerformed(ActionEvent e) {

            //判断是哪个按钮被点击

            if(e.getSource()==jb1)

            {

                //对用户点击添加按钮的响应动作

                System.out.println("用户想查询!");

                //...

                //查询数据库,更新JTable,这样大量的代码重复,解决办法:

                //把对表操作封装成一个类,可以完成对表的操作

                //把对表的数据装到StuModel中,就可以比较简单的完成查询

                String name=this.jtf.getText().trim();

                //写一个sql语句

                String sql="select * from stu where stuName=?";

                String paras[]={name};

                //构建新的数据模型类,并更新

                sm=new StuModel();

                

                sm.queryStu(sql, paras);

                //更新JTable

                jt.setModel(sm);

            }

            //当用户点击添加

            else if(e.getSource()==jb2)

            {

                StuAddDialog sa=new StuAddDialog(this,"添加学生",true);

                

                //重新再获得新的数据模型

                sm=new StuModel();

                String []paras2={"1"};

                sm.queryStu("select * from stu where 1=?", paras2);

                //更新JTable

                jt.setModel(sm);

            }

            else if(e.getSource()==jb3)

            {

                //户希望修改

                int rowNum=this.jt.getSelectedRow();

                if(rowNum==-1)

                {

                    //提示

                    JOptionPane.showMessageDialog(this, "请选择一行");

                    return ;

                }

                //显示修改对话框

                new StuUpdateDialog(this,"修改学生",true,sm,rowNum);

                

                //重新再获得新的数据模型

                sm=new StuModel();

                //更新JTable

                String []paras2={"1"};

                sm.queryStu("select * from stu where 1=?", paras2);

                jt.setModel(sm);

            }

            

            else if(e.getSource()==jb4)

            {

                //说明用户希望删除记录

                //1.得到该学生的id

                int rowNum=this.jt.getSelectedRow();//返回用户点中的行,如果没有选返返-1

                if(rowNum==-1)

                {

                    //提示

                    JOptionPane.showMessageDialog(this,"请选择一行");

                    return ;

                }

                //得到学生编号

                String stuId=(String)sm.getValueAt(rowNum, 0);

                //System.out.println("id="+stuId);

                

                //创建一个sql语句

                String sql="delete from stu where stuId=?";

                String []paras={stuId};

                StuModel temp=new StuModel();

                temp.updStu(sql,paras);

                

                //更新数据模型

                sm=new StuModel();

                String []paras2={"1"};

                sm.queryStu("select * from stu where 1=?", paras2);

                //更新JTable

                jt.setModel(sm);

            }

        }

    }

    min版学生管理系统3.0_Class20/ com.test2/ StuAddDialog.java

    package com.test2;

    import java.awt.BorderLayout;

    import java.awt.Frame;

    import java.awt.GridLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.beans.Statement;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import javax.swing.JButton;

    import javax.swing.JDialog;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    import javax.swing.JTextField;

    public class StuAddDialog extends JDialog implements ActionListener{

        //定义需要的控件

        JLabel jl1,jl2,jl3,jl4,jl5,jl6;

        JButton jb1,jb2;

        JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;

        JPanel jp1,jp2,jp3;

        

        //owner它的父窗口

        //title窗口名

        //modal指定模式窗口,还是非模式窗口

        public StuAddDialog(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("系别");

            

            jtf1=new JTextField();

            jtf2=new JTextField();

            jtf3=new JTextField();

            jtf4=new JTextField();

            jtf5=new JTextField();

            jtf6=new JTextField();

            

            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));

            

            //添加组件

            jp1.add(jl1);

            jp1.add(jl2);

            jp1.add(jl3);

            jp1.add(jl4);

            jp1.add(jl5);

            jp1.add(jl6);

            

            jp2.add(jtf1);

            jp2.add(jtf2);

            jp2.add(jtf3);

            jp2.add(jtf4);

            jp2.add(jtf5);

            jp2.add(jtf6);

            

            jp3.add(jb1);

            jp3.add(jb2);

            

            this.add(jp1,BorderLayout.WEST);

            this.add(jp2,BorderLayout.CENTER);

            this.add(jp3,BorderLayout.SOUTH);

              

            

            //展现对话框

            this.setSize(300,250);

            //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setTitle("添加学生");

            this.setVisible(true);

        }

        @Override

        public void actionPerformed(ActionEvent e) {

            if(e.getSource()==jb1)

            {

                //希望添加学生

                StuModel temp=new StuModel();

                String sql="insert into stu values(?,?,?,?,?,?)";

                String []paras={jtf1.getText(),jtf2.getText(),

                        jtf3.getText(),jtf4.getText(),

                        jtf5.getText(),jtf6.getText()};

                if(!temp.updStu(sql, paras))

                {

                    //提示

                    JOptionPane.showMessageDialog(this,"添加失败");

                }

                //关闭对话框

                this.dispose();

            }

        }

    }

    min版学生管理系统3.0_Class20/ com.test2/ StuUpdateDialog.java

    /*

    * 修改学生

    */

    package com.test2;

    import java.awt.BorderLayout;

    import java.awt.Frame;

    import java.awt.GridLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.beans.Statement;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import javax.swing.JButton;

    import javax.swing.JDialog;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JPanel;

    import javax.swing.JTextField;

    public class StuUpdateDialog extends JDialog implements ActionListener{

        //定义需要的控件

        JLabel jl1,jl2,jl3,jl4,jl5,jl6;

        JButton jb1,jb2;

        JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;

        JPanel jp1,jp2,jp3;

        

        //owner它的父窗口

        //title窗口名

        //modal指定模式窗口,还是非模式窗口

        public StuUpdateDialog(Frame owner,String title,boolean modal,StuModel sm,int rowNums)

        {

            super(owner,title,modal);//调用父类构造方法,达到模式对话框效果

            jl1=new JLabel("学号");

            jl2=new JLabel("姓名");

            jl3=new JLabel("性别");

            jl4=new JLabel("年龄");

            jl5=new JLabel("籍贯");

            jl6=new JLabel("系别");

            

            jtf1=new JTextField();

            //初始化数据

            jtf1.setText((String)sm.getValueAt(rowNums, 0));

            //jtf1,不能修改

            jtf1.setEditable(false);

            jtf2=new JTextField();

            jtf2.setText((String)sm.getValueAt(rowNums, 1));

            jtf3=new JTextField();

            jtf3.setText((String)sm.getValueAt(rowNums, 2));

            jtf4=new JTextField();

            jtf4.setText(sm.getValueAt(rowNums, 3).toString());

            jtf5=new JTextField();

            jtf5.setText((String)sm.getValueAt(rowNums, 4));

            jtf6=new JTextField();

            jtf6.setText((String)sm.getValueAt(rowNums, 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));

            

            //添加组件

            jp1.add(jl1);

            jp1.add(jl2);

            jp1.add(jl3);

            jp1.add(jl4);

            jp1.add(jl5);

            jp1.add(jl6);

            

            jp2.add(jtf1);

            jp2.add(jtf2);

            jp2.add(jtf3);

            jp2.add(jtf4);

            jp2.add(jtf5);

            jp2.add(jtf6);

            

            jp3.add(jb1);

            jp3.add(jb2);

            

            this.add(jp1,BorderLayout.WEST);

            this.add(jp2,BorderLayout.CENTER);

            this.add(jp3,BorderLayout.SOUTH);

              

            

            //展现对话框

            this.setSize(300,250);

            //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setTitle("修改学生");

            this.setVisible(true);

        }

        @Override

        public void actionPerformed(ActionEvent e) {

            if(e.getSource()==jb1)

            {

                    //做一个sql

                    //预编译的语句对象

                    String sql="update stu set stuName=?,stuSex=?," +

                            "stuAge=?,stuJg=?,stuDept=? where stuId=?";

                    String []paras={jtf2.getText(),jtf3.getText(),

                            jtf4.getText(),jtf5.getText(),

                            jtf6.getText(),jtf1.getText()};

                    StuModel temp=new StuModel();

                    temp.updStu(sql, paras);

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

                    

            }

        }

    }

    min版学生管理系统3.0_Class20/ com.test2/ StuModel.java

    /*

    * 这是一个stu表的模型

    * 可以把对stu表的各种操作封装到该模型中

    */

    package com.test2;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.util.Vector;

    import javax.swing.table.*;

    public class StuModel extends AbstractTableModel{

        //rowData用来存放行数据

        //columnNames存放列名

        Vector rowData,columnNames;

          

        

        //初始化连接数据库,查询,查询的本质就是初始化

        public void queryStu(String sql,String []paras)

        {

            SqlHelper sqlHelper=null;

            //中间

            columnNames=new Vector();

            //设置列名

            columnNames.add("学号");

            columnNames.add("名字");

            columnNames.add("性别");

            columnNames.add("年龄");

            columnNames.add("籍贯");

            columnNames.add("系别");

            

            rowData=new Vector();

            try {

                sqlHelper=new SqlHelper();

                ResultSet rs=sqlHelper.queryExectue(sql, paras);

                

                while(rs.next())

                {

                    //rowData可以存放多行

                    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

            {

                //关闭资源

                sqlHelper.close();

            }

        }

        

        //更新学生,添加学生(增、删、改)

        @SuppressWarnings("finally")

        public boolean updStu(String sql,String []paras)//通过函数传入参数解决

        {

            //根据用户输入的sql语句,完成添加...

            //创建SqlHelper(如果程序并发性不考虑,可以把SqlHelper做成static)

            SqlHelper sqlHelper=new SqlHelper();

            return sqlHelper.updExecute(sql, paras);

        }

        

        //做一个构造函数,用于初始化我们的数据模型

        public StuModel()

        {

            

        }

        

        @Override //得到共有多少列

        public int getColumnCount() {

            //System.out.println("getColumncount");

            return this.columnNames.size();

        }

        @Override //得到共有多少行

        public int getRowCount() {

            //

            return this.rowData.size();

        }

        @Override //得到某行某列的数据

        public Object getValueAt(int rowIndex, int columnIndex) {

            //return null;

            return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);

        }

        @Override

        public String getColumnName(int column) {

            // 重写这个方法,修改默认的列名

            return (String)this.columnNames.get(column);

        }

    }

    min版学生管理系统3.0_Class20/ com.test2/ SqlHelper.java

    /*

    * 这是一个对数据库进行操作的类(SqlHelper)

    */

    package com.test2;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    public class SqlHelper {

        

        //定义操作数据库需要的东西

        PreparedStatement ps=null;

        Connection ct=null;

        ResultSet rs=null;

        String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=LiangshanHeros";

        String user="sa";

        String passwd="luowei";

        String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";

        

        //关闭数据库资源

        public void close()

        {

            //关闭资源

            try {

                if(rs!=null) rs.close();

                if(ps!=null) ps.close();

                if(ct!=null) ct.close();

                

            } catch (final Exception e) {

                e.printStackTrace();

            }

        }

        //写一个不需要注入的方法

        public ResultSet queryExectue(String sql)

        {

            try {

                //1.加载驱动

                Class.forName(driver);

                //2.得到连

                ct=DriverManager.getConnection(url,user,passwd);

                //3.创建ps

                ps=ct.prepareStatement(sql);

                

                rs=ps.executeQuery();

            } catch (Exception e) {

                e.printStackTrace();

            }finally

            {

                //关闭资源,???

            }

            return rs;

        }

        

        //查询数据库操作

        public ResultSet queryExectue(String sql,String []paras)

        {

            try {

                //1.加载驱动

                Class.forName(driver);

                //2.得到连接

                ct=DriverManager.getConnection(url,user,passwd);

                //3.创建ps

                ps=ct.prepareStatement(sql);

                //ps的问号赋值

                for(int i=0;i<paras.length;i++)

                {

                    ps.setString(i+1, paras[i]);

                }

                rs=ps.executeQuery();

            } catch (Exception e) {

                e.printStackTrace();

            }finally

            {

                //关闭资源,???

            }

            return rs;

        }

        

        //把增、删、改合到一起

        public boolean updExecute(String sql,String []paras)

        {

            boolean b=true;

            try {

                //1.加载驱动

                Class.forName(driver);

                //2.得到连接

                ct=DriverManager.getConnection(url,user,passwd);

                //3.创建ps

                ps=ct.prepareStatement(sql);

                //ps的问号赋值

                for(int i=0;i<paras.length;i++)

                {

                    ps.setString(i+1, paras[i]);

                }

                //4.执行操作

                if(ps.executeUpdate()!=1)

                {

                    b=false;

                }

            } catch (Exception e) {

                b=false;

                e.printStackTrace();

            }finally

            {

                this.close();

            }

            return b;

        }

    }

  • 相关阅读:
    2017.4.6下午
    2017.4.6上午
    2017.3.31下午
    2017.4.5下午
    2017.4.5上午
    2017.4.1上午
    2017.3.31上午
    2017.3.28下午
    2017.3.28上午
    3.28上午
  • 原文地址:https://www.cnblogs.com/luowei010101/p/2138328.html
Copyright © 2020-2023  润新知