⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbc.java~5~

📁 学生信息管理系统java很好的参考价值
💻 JAVA~5~
📖 第 1 页 / 共 3 页
字号:
                                }
                        }
                        catch(Exception ex)
                        {
                                System.out.println(e);
                                JOptionPane.showMessageDialog(this,"未找到该课程的信息","课程查找对话框",JOptionPane.INFORMATION_MESSAGE);
                        }
                        finally
                        {
                                DB.closeStmt();
                                DB.closeConn();
                        }
                }
                if(e.getSource()==bt2)
                    setVisible(false);
        }
}

//课程查询
 class kccx   extends   JFrame  implements ActionListener
 {
         JLabel lb1;
         JTextField  tf1;
         JButton  bt1,bt2;
         kccx()
         {
                 setTitle("课程信息查询");
                 lb1=new JLabel("请输入课程名:");
                 tf1=new JTextField(10);
                 bt1=new JButton("查询");
                 bt2=new JButton("取消");

                 Container con=this.getContentPane();
                 con.setLayout(null);

                 lb1.setBackground(Color.cyan);
                 lb1.setFont(new Font("宋体",Font.BOLD,12));

                 tf1.setBackground(Color.yellow);
                 tf1.setFont(new Font("宋体",Font.BOLD,12));

                 bt1.setBackground(Color.green);
                 bt1.setFont(new Font("宋体",Font.BOLD,12));

                 bt2.setBackground(Color.orange);
                 bt2.setFont(new Font("宋体",Font.BOLD,12));

                 lb1.setBounds(80,80,120,30);
                 tf1.setBounds(200,80,80,30);
                 bt1.setBounds(80,130,100,30);
                 bt2.setBounds(180,130,100,30);

                 con.add(lb1);con.add(tf1);
                 con.add(bt1);con.add(bt2);
                 bt1.addActionListener(this);
                 bt2.addActionListener(this);
                 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    public  void  actionPerformed(ActionEvent e)
    {
                Database DB=new Database();
                String[] temp=new String[6];//存放单个学生各种信息
                StringBuffer mess=new StringBuffer();//存放所有查询的结果
                try
                {
                        String sql="select * from kecheng WHERE cname ='"+tf1.getText()+"'";
                        DB.OpenConn();
                        DB.rs = DB.executeQuery(sql);
                        if(DB.rs.next())
                        {
                                for(int i=1;i<4;i++)
                                temp[i]=DB.rs.getString(i);
                                JOptionPane.showMessageDialog(this,"找到课程信息"+"\n"+"课程名:"+temp[1]+"\n"+
                                                         "学分:"+temp[2]+"\n"+"学时:"+temp[3]+"\n","课程查找对话框",JOptionPane.INFORMATION_MESSAGE);
                        }
                        else
                        JOptionPane.showMessageDialog(this,"未找到该课程的信息","课程查找对话框",JOptionPane.INFORMATION_MESSAGE);
                }
                catch(Exception ex)
                {
                        System.out.println(e);
                        JOptionPane.showMessageDialog(null, "保存失败", "错误", JOptionPane.ERROR_MESSAGE);
                }
                finally
                {
                        DB.closeStmt();
                        DB.closeConn();
                }
                if(e.getSource()==bt2)
                    setVisible(false);
        }
}

//课程修改
class kcxg  extends   JFrame  implements ActionListener
{
        JLabel lb1,lb2,lb3;
        JTextField  tf1,tf2,tf3;
        JButton  bt1,bt2;
        kcxg()
        {
                setTitle("课程信息修改");
                this.getContentPane().setLayout(null);
                lb1=new JLabel("需改的课程");
                tf1=new JTextField(10);
                lb2=new JLabel("(新)学分");
                tf2=new JTextField(10);
                lb3=new JLabel("(新)学时");
                tf3=new JTextField(10);
                bt1=new JButton("修改");
                bt2=new JButton("取消");

                lb1.setBackground(Color.cyan);
                lb1.setFont(new Font("宋体",Font.BOLD,16));

                lb2.setBackground(Color.cyan);
                lb2.setFont(new Font("宋体",Font.BOLD,16));

                lb3.setBackground(Color.cyan);
                lb3.setFont(new Font("宋体",Font.BOLD,16));

                tf1.setBackground(Color.pink);
                tf1.setFont(new Font("宋体",Font.BOLD,16));

                tf2.setBackground(Color.pink);
                tf2.setFont(new Font("宋体",Font.BOLD,16));

                tf3.setBackground(Color.pink);
                tf3.setFont(new Font("宋体",Font.BOLD,16));

                bt1.setBackground(Color.green);
                bt1.setFont(new Font("楷书",Font.BOLD,16));

                bt2.setBackground(Color.green);
                bt2.setFont(new Font("楷书",Font.BOLD,16));

                lb1.setBounds(0,40,100,30);tf1.setBounds(100,40,100,30);
                lb2.setBounds(0,70,100,30);tf2.setBounds(100,70,100,30);
                lb3.setBounds(0,100,100,30);tf3.setBounds(100,100,100,30);
                bt1.setBounds(0,130,100,30);bt2.setBounds(130,130,100,30);

                Container con=this.getContentPane();
                con.add(lb1);con.add(tf1);
                con.add(lb2);con.add(tf2);
                con.add(lb3);con.add(tf3);
                con.add(bt1);con.add(bt2);
                bt1.addActionListener(this);
                bt2.addActionListener(this);
                this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }
        public  void  actionPerformed(ActionEvent e)
        {
                Database DB=new Database();
                if(e.getSource()==bt1)
                {
                        try
                        {

                            String sql="update kecheng Set cmark  ="+Integer.parseInt(tf2.getText())+
                                                       " and ctime=tf3.getText() WHERE cname='"+tf1.getText()+"'";
                                DB.OpenConn();
                                DB.executeUpdate(sql);
                            JOptionPane.showMessageDialog(this,"成功修改课程信息"+"\n"+"课程名:"+tf1.getText()+"\n"+
                                                            "学分:"+tf2.getText()+"\n"+"学时:"+tf3.getText()+"\n","课程修改对话框",JOptionPane.INFORMATION_MESSAGE);
                        }
                        catch(Exception ex){
                                System.out.println(e);
                                JOptionPane.showMessageDialog(this,"未找到该课程的信息","课程查找对话框",JOptionPane.INFORMATION_MESSAGE);
                        }
                        finally {
                                DB.closeStmt();
                                DB.closeConn();
                        }
                }
                if(e.getSource()==bt2)
                    setVisible(false);
     }
}

class Database {

        private Statement stmt=null;
        ResultSet rs=null;
        private Connection conn=null;
        String sql;
        String strurl="jdbc:odbc:student";

        public Database(){
        }

        /** 打开数据库连接*/
        public void OpenConn()throws Exception{
                try{
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        conn=DriverManager.getConnection(strurl);
                }
                catch(Exception e){
                        System.err.println("OpenConn:"+e.getMessage());
                }
        }

        /*执行sql语句,返回结果集rs*/
        public ResultSet executeQuery(String sql){
                stmt = null;
                rs=null;
                try{
                        stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                        rs=stmt.executeQuery(sql);
                }
                catch(SQLException e){
                        System.err.println("executeQuery:"+e.getMessage());
                }
                return rs;
        }

        /* 执行sql语句*/
        public void executeUpdate(String sql){
                stmt=null;
                rs=null;
                try{
                        stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                        stmt.executeQuery(sql);
                        conn.commit();
                }
                catch(SQLException e){
                        System.err.println("executeUpdate:"+e.getMessage());
                }
        }

        public void closeStmt(){
                try{
                        stmt.close();
                }
                catch(SQLException e){
                        System.err.println("closeStmt:"+e.getMessage());
                }
        }

        /* 关闭数据库连接*/

        public void closeConn(){
                try{
                        conn.close();
                }
                catch(SQLException ex){
                        System.err.println("aq.closeConn:"+ex.getMessage());
                }
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -