get value from database by searching
Hi. Sorry if such a question was asked before. I am coding an application for entering child patient details as registration. The basic details were entered in a previous form. Now i am adding the diagnosis. here i have a problem. i require that when i type the id into a text box and press search another textbox should return the associated name. i got code for it but it doesn't work here is the code i used
Code :
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/autmgt","root","pass");
Statement st = conn.createStatement();
ResultSet rs=null;
String pval1=text1.getText();
String SQL="select * from child where CID="+"'"+pval1+"'";
rs=st.executeQuery(SQL);
String k=rs.getString("CName");
text2.setText(k);
}catch(Exception e){}
TIA
Re: get value from database by searching
"but it doesn't work" doesn't give us much to go on. What problems are you having? Are you getting error messages? Is your program not behaving well? Also consider fixing your code indentation and make it more uniform, usually 3 spaces suffices plenty. It's hard enough trying to figure out someone else's code that you really don't want to make it harder for us by posting poorly formatted code.
Luck!
Re: get value from database by searching
sorry for that. no i dont get any errors. when i type say 123(which should return bob) and press search, nothing happens.
Re: get value from database by searching
anyone??? i really need help on this.
Re: get value from database by searching
ok solved it was a syntax error here is the code i used
Code :
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/autmgt","root","K13nn0r");
Statement st = conn.createStatement();
ResultSet rs=null;
String pval1=text1.getText();
String SQL="select * from child where CID='"+pval1+"'";
rs=st.executeQuery(SQL);
// String k=rs.getString(2);
while(rs.next()){
text2.setText(rs.getString("CName"));
}
}catch(Exception e){}