2014-03-25 110 views
-2

我必須將ms訪問數據庫中的數據顯示到標籤中。表包含這些列名從java中訪問數據的數據

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

用戶選擇組合框的值,其是從表ID和相應顯示相應的數據。錯誤是:

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. 
} 

回答

1

首先,使用PreparedStatements

其次...周圍的值引號您的ID,'+i+'將使訪問(以及幾乎所有數據庫)將該值視爲String,我懷疑它是。嘗試刪除引號

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

第三...像素完美的佈局是現代用戶界面開發中的錯覺。您不控制不同平臺的渲染管道。這將把你的像素完美的形式,變成亂碼。

Swing的設計與佈局管理器一起工作,它們使您能夠專注於工作流程,而不是處理DPI,字體大小和其他渲染問題的每種可能組合。

花時間學習和使用合適的佈局管理器,並看看Laying Out Components Within a Container瞭解更多詳情