使用JavaSwing界面实现数据库基本操作

文章作者 100test 发表时间 2007:09:20 13:35:49
来源 100Test.Com百考试题网


实例:用Java Swing图形化界面来对数据库操作(增,删,查,改)

数据库版本:SQLServer2000

数据库名:dxaw

用户名:dxaw

密码:123

表名:bankAccount

表结构:id(int[自动增长]),ownerName(varchar),accountValue(varchar),accountLevel(varchar)

程序代码:

import java.awt.event.ActionEvent.

            import java.awt.event.ActionListener.

            import java.sql.*.

            import java.awt.*.

            import javax.swing.*.

            public class data extends JFrame implements ActionListener{

            JButton add,0select,del,0update.

            JTable table.

            Object body[][]=new Object[50][4].

            String title[]={"编号","姓名","分数","级别"}.

            Connection conn.

            Statement stat.

            ResultSet rs.

            JTabbedPane tp.

            public data() {

            super("数据库操作").

            this.setSize(400,300).

            this.setLocation(300,200).

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).

            JPanel ps=new JPanel().

            add=new JButton("添加").

            0select=new JButton("显示").

            0update=new JButton("更改").

            del=new JButton("删除").

            add.addActionListener(this).

            0select.addActionListener(this).

            0update.addActionListener(this).

            del.addActionListener(this).

            ps.add(add).ps.add(0select).ps.add(0update).ps.add(del).

            table=new JTable(body,title).

            tp=new JTabbedPane().

            tp.add("bankAccount表",new JScrollPane(table)).

            this.getContentPane().add(tp,"Center").

            this.getContentPane().add(ps,"South").

            this.setVisible(true).

            this.connection().

            }

            public void connection(){

            try {

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

            String url="jdbc:microsoft:sqlserver://localhost:1433.DatabaseName=dxaw".

            conn=DriverManager.getConnection(url,"dxaw","123").

            stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

            ResultSet.CONCUR_READ_ONLY).

            } catch (Exception ex) {

            }

            }

            public static void main(String[] args) {

            data data = new data().

            }

            public void actionPerformed(ActionEvent e) {

            if(e.getSource()==add)

            {add().}

            if(e.getSource()==0select)

            {0select().}

            if(e.getSource()==0update)

            {0update().}

            if(e.getSource()==del)

            {del().}

            }

            public void del() {

            try {

            int row=table.getSelectedRow().

            stat.executeUpdate("0delete bankAccount where accountID=" body[row][0] "").

            JOptionPane.showMessageDialog(null,"数据已成功删除").

            this.0select().

            } catch (SQLException ex) {

            }

            }

            public void 0update() {

            try {

            int row=table.getSelectedRow().

            JTextField t[]=new JTextField[6].

            t[0]=new JTextField("输入姓名:").

            t[0].setEditable(false).

            t[1]=new JTextField().

            t[2]=new JTextField("输入分数:").

            t[2].setEditable(false).

            t[3]=new JTextField().

            t[4]=new JTextField("输入级别:").

            t[4].setEditable(false).

            t[5]=new JTextField().

            String but[]={"确定","取消"}.

            int go=JOptionPane.showOptionDialog(

            null,t,"插入信息",JOptionPane.YES_OPTION,

            JOptionPane.INFORMATION_MESSAGE,null,but,but[0]).

            if(go==0){

            String ownerName=new String(t[1].getText().getBytes("ISO-8859-1"),

            "GBK").

            String accountValue=t[3].getText().

            int accountLevel=Integer.parseInt(t[5].getText()).

            stat.executeUpdate("0update bankAccount set ownerName=" ownerName ",

            accountValue=" accountValue ",accountLevel=" accountLevel "

            where accountID=" body[row][0] "").

            JOptionPane.showMessageDialog(null,"修改数据成功").

            this.0select().

            }

            } catch (Exception ex) {

            }

            }

            public void 0select() {

            try {

            for(int x=0.x.length.x  ){

            body[x][0]=null.

            body[x][1]=null.

            body[x][2]=null.

            body[x][3]=null.

            }

            int i=0.

            rs=stat.executeQuery("0select * from bankAccount").

            while(rs.next()){

            body[i][0]=rs.getInt(1).

            body[i][1]=rs.getString(2).

            body[i][2]=rs.getString(3).

            body[i][3]=rs.getInt(4).

            i=i 1.

            }

            this.repaint().

            } catch (SQLException ex) {

            }

            }

            private void add() {

            try {

            JTextField t[]=new JTextField[6].

            t[0]=new JTextField("输入姓名:").

            t[0].setEditable(false).

            t[1]=new JTextField().

            t[2]=new JTextField("输入分数:").

            t[2].setEditable(false).

            t[3]=new JTextField().

            t[4]=new JTextField("输入级别:").

            t[4].setEditable(false).

            t[5]=new JTextField().

            String but[]={"确定","取消"}.

            int go=JOptionPane.showOptionDialog(null,t,"插入信息",

            JOptionPane.YES_OPTION,JOptionPane.INFORMATION_MESSAGE,null,but,but[0]).

            if(go==0){

            try{

            String ownerName=new String(t[1].getText().getBytes("ISO-8859-1"),"GBK").

            String accountValue=t[3].getText().

            int accountLevel=Integer.parseInt(t[5].getText()).

            stat.executeUpdate("insert into bankAccount

            (ownerName,accountValue,accountLevel) values

            (" ownerName "," accountValue "," accountLevel ")").

            JOptionPane.showMessageDialog(null,"数据已成功插入!").

            }catch(Exception ee){

            JOptionPane.showMessageDialog(null,"插入数据错误!").

            }

            }

            } catch (Exception ex) {

            }

            }

            }


相关文章


使用JavaSwing界面实现数据库基本操作
使用String. lit方法时要注意的问题
使用JavaJDK中Java.net包控制UDP协议
比较Hashtable类和HashMap类的不同之处
使用JavaScript来实现浏览器菜单的命令
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛