sql - Data fetching from ms access in java -
i have display data ms access database labels . table contain these column names
id(long integer), name(text),) gender(text), address(text), city (text). users select combo box values ids table , 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 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 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 = (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 * studentdetails 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 preparedstatements
second...surrounding id value in quotes, '+i+' make access (and databases) treat value string, doubt is. try removing quotes
string st = "select * studentdetails id =" + i; thirdly...pixel perfect layouts illusion in modern ui development. don't control rendering pipeline of different platforms. take pixel perfect form , turn gibberish.
swing has been designed work layout managers , these provide ability focus on work flow , not on dealing each every possible combination of dpi, font sizes , other rendering issues.
take time learn , use appropriate layout managers , take @ laying out components within container more details
Comments
Post a Comment