简体   繁体   中英

Data fetching from ms access in java

I have to display data from ms access database to labels . Table contain these column names

ID(Long Integer), 
Name(Text),)
Gender(Text),
Address(Text), 
City (Text). 

Users select combo box values which are IDs from table and correspondingly respective data appears. Error is:

sucess sssssucess

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

  package studentdetails;

  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.util.*;
  import java.sql.*;
  import java.awt.event.KeyAdapter.*;
  import java.awt.event.ActionListener.*;
  import javax.swing.AbstractButton.*;
  import java.text.SimpleDateFormat;
  import java.util.logging.Level;
  import java.util.logging.Logger;

 class StudentDetails extends JFrame implements ActionListener,KeyListener
  {
   JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9;
   JLabel ll1,ll2,ll3,ll4,ll5,ll6,ll7;
   JButton b1,b2,b3;
   JComboBox c1;
   Connection conn;


public StudentDetails()
{
    super("Institute Management System");

    l1=new JLabel("ID::");
    l2=new JLabel("STUDENT NAME::");
    l3=new JLabel("GENDER::");
    l4=new JLabel("ADDRESS::");
    l5=new JLabel("CITY::");
    l6=new JLabel("CONTACT NO.::");
    //l7=new JLabel("DATE OF JOINING::");
    l8=new JLabel("STUDENT DETAILS::");
    l9=new JLabel("SELECT THE STUDENT ID:");

    ll1  = new JLabel();
            ll2  = new JLabel();
            ll3  = new JLabel();
            ll4  = new JLabel();
            ll5  = new JLabel();
            ll6  = new JLabel();
            //ll7  = new JLabel();



    b1=new JButton("VIEW");
    b2=new JButton("BACK");
    b3=new JButton("CLEAR");


    c1=new JComboBox();


    add(l8);
    l8.setBounds(210,30,150,80);

    add(l9);
    l9.setBounds(55,75,150,80);

    add(c1);
    c1.setBounds(250,100,150,30);

    add(l1);
    l1.setBounds(120,150,150,30);
    add(ll1);
    ll1.setBounds(250,150,150,30);

    add(l2);
    l2.setBounds(120,200,150,30);
    add(ll2);
    ll2.setBounds(250,200,150,30);

    add(l3);
    l3.setBounds(120,250,150,30);
    add(ll3);
    ll3.setBounds(250,250,150,30);

    add(l4);
    l4.setBounds(120,300,150,30);
    add(ll4);
    ll4.setBounds(250,300,150,30);

    add(l5);
    l5.setBounds(120,350,150,30);
    add(ll5);
    ll5.setBounds(250,350,150,30);

    /*add(l6);
    l6.setBounds(120,400,150,30);
    add(ll6);
    ll6.setBounds(250,400,150,30);

    add(l7);
    l7.setBounds(120,450,150,30);
    add(ll7);
    ll7.setBounds(250,450,150,30);
    */

    add(b1);
    b1.setBounds(500,100,80,30);
    add(b2);
    b2.setBounds(200,520,80,30);
    add(b3);
    b3.setBounds(450,520,80,30);



    setSize(700,700);
    setBackground(Color.BLUE);
    setLayout(null);
    setVisible(true);
    setExtendedState(MAXIMIZED_BOTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);

            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                conn=DriverManager.getConnection("jdbc:odbc:LANGUAGEDSN");
                System.out.println("sucess");
                Statement stmt=conn.createStatement();
                String str = "Select ID from STUDENTDETAILS";
                ResultSet rs = stmt.executeQuery(str);
                while(rs.next())
                {
                     c1.addItem(rs.getString(1));
                }
            } 
            catch(Exception e)
            {
                System.out.println(e);
    }

}



public void actionPerformed(ActionEvent ae)
{
        Object ob=ae.getSource();

        String i = (String)c1.getSelectedItem();
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd  HH:mm", Locale.getDefault());
        //String sdt_ = sdf.format(dt_);

        if(ob==b1)
        {

            try
    {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                conn=DriverManager.getConnection("jdbc:odbc:LANGUAGEDSN");
                Statement stmt=conn.createStatement();

                String st = "Select * from STUDENTDETAILS where ID ='"+i+"'";
                System.out.println("sssssucess");
                ResultSet rs = stmt.executeQuery(st);
                while(rs.next())
                {


                   l1.setText(rs.getString(1));

                    ll2.setText(rs.getString(2));
                    ll3.setText(rs.getString(3));
                    ll4.setText(rs.getString(4));
                    ll5.setText(rs.getString(5));


                }
            }
            catch(Exception e)
            {
                    System.out.println(e);
            }
        }

        else if(ob==b2)
        {
            ll1.setText("");
            ll2.setText("");
            ll3.setText("");
            ll4.setText("");
            ll5.setText("");
            //ll6.setText("");
            //ll7.setText("");
        }
    }





public static void main(String args[])
{
    new StudentDetails();
}

@Override
public void keyTyped(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void keyPressed(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void keyReleased(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

First of all, use PreparedStatement s

Second...surrounding your id value in quotes, '+i+' will make Access (and just about all databases) treat the value as a String , which I doubt it is. Try removing the quotes

String st = "Select * from STUDENTDETAILS where ID =" + i;

Thirdly...Pixel perfect layouts are an illusion in modern UI development. You don't control the rendering pipeline of different platforms. This will take your pixel perfect form and turn into gibberish.

Swing has been designed to work with layout managers and these provide you with the ability to focus on work flow and not on dealing to each an every possible combination of DPI, font sizes and other rendering issues.

Take the time to learn and use appropriate layout managers and take a look at Laying Out Components Within a Container for more details

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