java连接access数据库初接触和遇到的问题(一)

本文介绍了使用Java连接Access数据库进行学生信息管理时遇到的问题,包括学号、姓名等信息录入、成绩录入、查询、删除和更新等操作。通过展示InputInfo、InputScore等类的代码,揭示了数据录入界面的设计和数据库操作。在实际运行中,遇到了'找不到文件'(未知的)'的SQLException,提示数据库文件可能未正确指定或不存在。
摘要由CSDN通过智能技术生成

在网上闲逛发现了一个学生信息管理系统,看过后我copy了下来,并且准备修改一下。

源代码如下

1、InputInfo.java …………………………资料录入;

2、InputScore.java …………………………成绩录入;

3、QueryInfo.java …………………………个人资料查询;

4、QueryInfoAll.java …………………………所有资料查询;

5、QueryScore.java …………………………个人成绩查询;

6、QueryScoreAll.java ……………………所有成绩查询;

7、DeleteInfo.java …………………………资料删除;

8、DeleteScore.java ……………………成绩删除;

9、UpdateInfo.java …………………………资料更改;

10、UpdateScore.java …………………………成绩更改;

11、Student.java …………………………学生类,实现与数据库学生资料表的连接;

12、Score.java ………………………………成绩类,实现与数据库学生成绩表的连接;

13、StudentMenu.java ……………………主界面,实现对各个子功能模块的调用。

1、InputInfo.java …………………………资料录入;

import java.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

import java.sql.*;

importjavax.swing.border.*;

 

public classInputInfo extends JFrame implements ActionListener  {

    //此处定义录入窗口需要的控件

    JComboBoxjcb;

    JRadioButtonrb1,rb2;

    JLabellbId,lbName,lbSex,lbAge,lbPhone,lbAdress;    

    JTextFieldtxtId,txtName,txtSex,txtAge,txtPhone,txtAdress;

    JButtonbtOk,btClear;  

    Studentst;

    InputInfo(Stringmsg){

       super(msg);                        

    }

    publicvoid go(){

       // 创建控件,布局并将所有的组件放到一个面板pa中             

       lbId=newJLabel("学号:");

       lbName=newJLabel("姓名:");

       lbSex=newJLabel("性别:");

       lbAge=newJLabel("年龄:");

       lbPhone=newJLabel("联系方式:");

       lbAdress=newJLabel("籍贯:");

 

       txtId=newJTextField(20);

       txtName=newJTextField(20);

       txtPhone=newJTextField(20);

       txtAge=newJTextField(20);

       rb1=newJRadioButton("男");

       rb2=newJRadioButton("女");

       String[]itemList={"桂林","阳朔","龙胜","合浦","西藏","湖北","黑龙江"};

       jcb=newJComboBox(itemList);

         jcb.setEditable(true);

         btOk=new JButton("确定");

         btOk.addActionListener(this);

         btClear= new JButton("重填");

         btClear.addActionListener(this);

 

       ButtonGroupgroup1 = new ButtonGroup();

       group1.add(rb1);

       group1.add(rb2);

            

       JPanelp1 =new JPanel();

       p1.setLayout(newGridLayout(0,2));

       p1.add(rb1);

       p1.add(rb2);

 

      JPanelpj1 =new JPanel();

    Borderetched = BorderFactory.createEtchedBorder();

      Borderborder = BorderFactory.createTitledBorder(etched, "");

    pj1.setBorder(border);

      //pj1.setLayout(newGridLayout(0,1));

      //pj1.setSize(1,1);

      pj1.add(btOk);

 

      JPanelpj2 =new JPanel();

      border= BorderFactory.createTitledBorder(etched, "");

      pj2.setBorder(border);

    //pj2.setLayout(newGridLayout(0,1));

      //pj2.setSize(1,1);

      pj2.add(btClear);

      

       JPanelp2 =new JPanel();

       border= BorderFactory.createTitledBorder(etched, "输入学生信息");

       p2.setBorder(border);

       p2.setLayout(newGridLayout(0,2));

       p2.add(lbId);p2.add(txtId);

       p2.add(lbName);p2.add(txtName);

       p2.add(lbSex);p2.add(p1);

       p2.add(lbAge);p2.add(txtAge);

       p2.add(lbPhone);p2.add(txtPhone);

       p2.add(lbAdress);p2.add(jcb);

         p2.add(pj1);p2.add(pj2);

 

         JPanelpa =new JPanel();// 将标签和输入控件面板放到pa

         pa.setLayout(newGridLayout(0,1));

         pa.add(p2);

     

         this.setContentPane(pa);  //替换录入窗口的内容

        this.setBounds(250,200,240,172);

       setSize(450,300);        

         show();      

    }

     

    publicvoid actionPerformed(ActionEvent e){

       // 从控件中获取用户输入

           intflag=0;

         if(e.getActionCommand()=="确定") {

             Stringid=txtId.getText();

                     if(id==null||id.equals("")){

                            JOptionPane.showMessageDialog(this,"学号不能为空!","错误提示",JOptionPane.WARNING_MESSAGE);

                            flag=1;

             }

             Stringname=txtName.getText();

                     if(name==null||name.equals("")){

                            JOptionPane.showMessageDialog(this,"姓名不能为空!","错误提示",JOptionPane.WARNING_MESSAGE);

                             flag=1;

             }

             Stringsex="";

             if(rb1.isSelected())

                sex="男";

             elseif(rb2.isSelected())

                sex="女";

                     if(sex==null||sex.equals("")){

                            JOptionPane.showMessageDialog(this,"请选择性别!","错误提示",JOptionPane.WARNING_MESSAGE);

                             flag=1;

             }

                     intage=0;

                     try{

             age=Integer.parseInt(txtAge.getText());

                     }catch(Exceptiona) {JOptionPane.showMessageDialog(this,"请输入年龄!","错误提示",JOptionPane.WARNING_MESSAGE); flag=1;}

             Stringphone=txtPhone.getText();

                     if(phone==null||phone.equals("")){

                            JOptionPane.showMessageDialog(this,"务必留下你得联系方式!","错误提示",JOptionPane.WARNING_MESSAGE);

                             flag=1;

             }

             Stringadress=(String)jcb.getSelectedItem();

                     if(adress==null||adress.equals("")){

                            JOptionPane.showMessageDialog(this,"请选择地址!","错误提示",JOptionPane.WARNING_MESSAGE);

                             flag=1;

             }

              

             //根据输入生成学生实例

             st=new Student(id,name,sex,age,phone,adress);

             Stringsql="INSERT INTOstudent(id,name,sex,age,phone,adress)VALUES('"+st.getId()+"','"+st.getName()+"','"+st.getSex()+"',"+st.getAge()+",'"+st.getPhone()+"','"+st.getAdress()+"')";

             if(flag==0){

             try{

                   Stringstrurl="jdbc:odbc:driver={Microsoft Access Driver(*.mdb)};DBQ=student.mdb";

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

                         Connectionconn=DriverManager.getConnection(strurl);

                         Statementstmt=conn.createStatement();

                         intrs=stmt.executeUpdate(sql);

                            JOptionPane.showMessageDialog(this,"资料录入成功!","提示",JOptionPane.INFORMATION_MESSAGE);

 

                 }catch(Exceptiona) {System.out.println(a);}

             hide();}

          }elseif(e.getActionCommand()=="重填") {

             txtId.setText("");

             txtName.setText("");

             txtAge.setText("");

             txtPhone.setText("");

        }   

    }

}

2、InputScore.java …………………………成绩录入;

import java.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

import java.sql.*;

importjavax.swing.border.*;

 

public classInputScore extends JFrame implements ActionListener  {

    //此处定义录入窗口需要的控件

    JLabellbId,lbJava,lbWeb,lbData,lbC,lbOpra,lbInfo;   

    JTextFieldtxtId,txtJava,txtWeb,txtData,txtC,txtOpra,txtInfo;

    JButtonbtOk,btClear;  

    Scoresc;

 

    InputScore(Stringmsg){

       super(msg);                        

    }

    publicvoid go(){

       // 创建控件,布局并将所有的组件放到一个面板pa中

         lbId=newJLabel("学号:");        

       lbJava=newJLabel("Java程序设计:");

       lbWeb=newJLabel("Web开发技术:");

       lbData=newJLabel("数据结构:");

       lbC=newJLabel("C语言:");

       lbOpra=newJLabel("操作系统:");

       lbInfo=newJLabel("信息系统分析与设计:");

 

       txtId=newJTextField(20);

       txtJava=newJTextField(20);

       txtWeb=newJTextField(20);

       txtData=newJTextField(20);

         txtC=newJTextField(20);

         txtOpra=newJTextField(20);

         txtInfo=newJTextField(20);

 

         btOk=new JButton("确定");

         btOk.addActionListener(this);

         btClear= new JButton("重填");

         btClear.addActionListener(this);

 

   JPanelpj1 =new JPanel();

      Borderetched = BorderFactory.createEtchedBorder();

       Borderborder = BorderFactory.createTitledBorder(etched, "");

      pj1.setBorder(border);

      //p2.setSize(20,30);

      pj1.add(btOk);

 

      JPanelpj2 =new JPanel();

      border= BorderFactory.createTitledBorder(etched, "");

      pj2.setBorder(border);

      //p2.setSize(20,30);

      pj2.add(btClear);

 

       JPanelp1 =new JPanel();

       border= BorderFactory.createTitledBorder(etched, "输入学生成绩");

       p1.setBorder(border);

       p1.setLayout(newGridLayout(0,2));

       p1.add(lbId);p1.add(txtId);

       p1.add(lbJava);p1.add(txtJava);

       p1.add(lbWeb);p1.add(txtWeb);

       p1.add(lbData);p1.add(txtData);

       p1.add(lbC);p1.add(txtC);

       p1.add(lbOpra);p1.add(txtOpra);

         p1.add(lbInfo);p1.add(txtInfo);

         p1.add(pj1);p1.add(pj2);

 

         JPanelpa =new JPanel();// 将标签和输入控件面板放到pa

         pa.setLayout(newGridLayout(0,1));

         pa.add(p1);

     

         this.setContentPane(pa);  //替换录入窗口的内容

              this.setBounds(250,200,240,172);

       setSize(450,300);        

         show();      

    }

     

    publicvoid actionPerformed(ActionEvent e){

       // 从控件中获取用户输入

           intflag=0;

         if(e.getActionCommand()=="确定") {

             Stringid=txtId.getText();

                     if(id==null||id.equals("")){

                            JOptionPane.showMessageDialog(this,"学号不能为空!","错误提示",JOptionPane.WARNING_MESSAGE);

                            flag=1;

             }

                     intjava=0;

             intweb=0;

             intdata=0;

             intc=0;

             intopra=0;

             intinfo=0;

 

                     try{

               java=Integer.parseInt(txtJava.getText());

               web=Integer.parseInt(txtWeb.getText());

               data=Integer.parseInt(txtData.getText());

               c=Integer.parseInt(txtC.getText());

               opra=Integer.parseInt(txtOpra.getText());

               info=Integer.parseInt(txtInfo.getText());

              }catch(Exceptiona) { JOptionPane.showMessageDialog(this,"请输入所有课程的成绩!","错误提示",JOptionPane.WARNING_MESSAGE);flag=1;}

             //根据输入生成学生实例

             sc= new Score(id,java,web,data,c,opra,info);

             Stringsql="INSERT INTO score(id,Java程序设计,Web开发技术,数据结构,C语言,操作系统,信息系统分析与设计)VALUES('"+sc.getId()+"',"+sc.getJava程序设计()+","+sc.getWeb开发技术()+","+sc.get数据结构()+","+sc.getC语言()+","+sc.get操作系统()+","+sc.get信息系统分析与设计()+")";

             if(flag==0){

             try{

                        Stringstrurl="jdbc:odbc:driver={Microsoft Access Driver(*.mdb)};DBQ=student.mdb";

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

                        Connectionconn=DriverManager.getConnection(strurl);

                        Statementstmt=conn.createStatement();

                        intrs=stmt.executeUpdate(sql);

                           JOptionPane.showMessageDialog(this,"成绩录入成功!","提示",JOptionPane.INFORMATION_MESSAGE);

 

                  }catch(Exceptiona) {System.out.println(a);}

             hide();  //隐藏录入窗口

                     }

         }

        elseif(e.getActionCommand()=="重填") {

             txtId.setText("");

             txtJava.setText("");

             txtWeb.setText("");

             txtData.setText("");

             txtC.setText("");

             txtOpra.setText("");

             txtInfo.setText("");

        }   

    }

}

3、QueryInfo.java …………………………个人资料查询;

import java.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjavax.swing.border.*;

import java.sql.*;

 

public classQueryInfo extends JFrame implements ActionListener  {

    //此处定义查询窗口需要的控件,包括查询条件和查询结果显示

    JComboBoxjcb;   

    JLabellbId;   

    JTextFieldtxtId;

    JCheckBoxcbId,cbName,cbSex,cbAge,cbPhone,cbAdress;

    JTextAreata;

    JButtonbtOk,btClear;

   

    QueryInfo(Stringmsg){

      super(msg);                          

    }

    publicvoid go(){

      // 创建控件,布局并将所有的组件放到一个面板pa中

      lbId=newJLabel("请输入学生的学号:");

      txtId=newJTextField(20);    

 

      cbId=newJCheckBox("学号");

      cbId.setSelected(true);

      cbName=newJCheckBox("姓名");

      cbName.setSelected(true);

      cbSex=newJCheckBox("性别");

      cb

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值