简体   繁体   中英

MS Access Database not responding

I am retrieving details from table made in MS Access Database.

User enters some name (being same name as present in table) in Textfield , then by hitting on submit button, details like FName,Studentid,Branch,Year,Semester,Email and Contact No is displayed.

My Java File is getting compiled correctly, but fetching details from table is not successfull.

The code is:

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)
        {
        }   
    }
}

}

Please help what is wrong in code.....

When you "glued together" your SQL statement you didn't put any quotes around the string value in the WHERE clause. However, you shouldn't be "gluing together" SQL statements anyway. You should be using a parameter query, something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM