MS Access数据库没有响应

时间:2014-01-21 00:10:13

标签: java sql ms-access

我正在从MS Access数据库中创建的表中检索详细信息。

用户输入一些名字 (在表格中显示同名),然后点击提交按钮,显示FName,Studentid,Branch,Year,Semester,Email和Contact No等详细信息。

我的Java文件正在正确编译,但从表中获取详细信息并不成功。

代码是:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;

class Data extends JFrame 
{
    JFrame f;
    JTabbedPane t;
    Data()
    {
        f = new JFrame("Data");
        f.setBounds(0,0,1300,500);
        t = new JTabbedPane();
        t.addTab("View", new View());
        f.add(t);
        f.setVisible(true);
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    }
    public static void main(String args[])
    {
        Data data = new Data();
    }
}
class View extends JPanel implements ActionListener
{
    JLabel id,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12;
    JButton b1;
    JTextField tf1;
    JPanel p1,p2;
    View()
    {
        setLayout(null);
        p1 = new JPanel();
        p1.setBounds(0,0,1300,100);
        p1.setBackground(Color.red);
        p2 = new JPanel();
        p2.setLayout(null);
        p2.setBounds(0,100,1300,400);
        p2.setBackground(Color.blue);
        id=new JLabel("Student Name");
        id.setBounds(500,100,100,50);
        b1=new JButton("SUBMIT");
        b1.setBounds(600,150,100,50);
        tf1=new JTextField(20);
        tf1.setBounds(600,100,100,50);
        p1.add(id);
        p1.add(tf1);
        b1.addActionListener(this);
        p1.add(b1);
        add(p1);
        add(p2);

        l1=new JLabel();
        l2=new JLabel();
        l3=new JLabel();
        l4=new JLabel();
        l5=new JLabel();
        l6 = new JLabel();
        l7=new JLabel("FName");
        l8=new JLabel("Branch");
        l9=new JLabel("Year");
        l10=new JLabel("Semester");
        l11=new JLabel("Email");
        l12=new JLabel("Contact No");

        l1.setBounds(700,10,100,20);
        l2.setBounds(700,40,100,20);
        l3.setBounds(700,70,100,20);
        l4.setBounds(700,100,100,20);
        l5.setBounds(700,130,100,20);
        l6.setBounds(700,160,100,20);
        l7.setBounds(500,10,100,20);
        l8.setBounds(500,40,100,20);
        l9.setBounds(500,70,100,20);
        l10.setBounds(500,100,100,20);
        l11.setBounds(500,130,100,20);
        l12.setBounds(500,160,100,20);


        p2.add(l1);
        p2.add(l2);
        p2.add(l3);
        p2.add(l4);
        p2.add(l5);
        p2.add(l6);
        p2.add(l7);
        p2.add(l8);
        p2.add(l9);
        p2.add(l10);
        p2.add(l11);
        p2.add(l12);

        p1.setVisible(true);
        p2.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
    String nm="0",br="0",yr ="0",sm="0",em="0",ph="0";
    int p=0;
    if(ae.getSource()==b1)
    {
        try
        {
            String ss=tf1.getText();
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("JDBC:ODBC:MS Access Database","","");
            Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet rs=st.executeQuery("Select * from information where FName= "+ss+" ");
            System.out.println(rs);
            while(rs.next())
            {
                nm=rs.getString("FName");
                br=rs.getString("Branch");
                yr=rs.getString("Year");
                sm=rs.getString("Semester");
                em=rs.getString("Email");
                ph=rs.getString("Contact No");
                p=Integer.parseInt(ph);

                l1.setText(nm);
                l2.setText(br);
                l3.setText(yr);
                l4.setText(sm);
                l5.setText(em);
                l6.setText(ph);
            }
            con.close();
        }

        catch(Exception e)
        {
        }   
    }
}

}

请帮助代码中的错误.....

1 个答案:

答案 0 :(得分:0)

当你"粘在一起"您的SQL语句没有在WHERE子句中的字符串值周围加上任何引号。但是,你不应该粘在一起"无论如何SQL语句。你应该使用参数查询,像这样

PreparedStatement st = con.prepareStatement(
        "Select * from information where FName=?",
        ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
st.setString(1, ss);
ResultSet rs = st.executeQuery();