【问题标题】:MS Access Database not respondingMS Access 数据库没有响应
【发布时间】:2014-01-21 00:10:12
【问题描述】:

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

用户输入一些名字 (与表格中的名称相同)在 Textfield 中,然后通过点击提交按钮,会显示 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)
        {
        }   
    }
}

}

请帮忙看看代码有什么问题.....

【问题讨论】:

  • 尝试在控制台打印堆栈跟踪:
  • 确保:jdbc url 是小写的:DriverManager.getConnection("jdbc:odbc:","","");

标签: java sql ms-access


【解决方案1】:

当您“粘合在一起”您的 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 2011-10-27
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多