Query MySQL using JSP and Servlet POST method -


i'm not sure if i'm trying "print" result correctly. i'm trying use jsp take in variable , pass servlet query database , display result. tested querying database in different java file , had no trouble that. advice appreciated!

jsp file:

<form method="post" action="helloservlet"/> enter name:<input name = "exname"/><br>  <input type = "submit" /> </form> 

java file (servlet):

protected void dopost(httpservletrequest request, httpservletresponse response) throws  servletexception, ioexception {          //response.setcontenttype("text/html; charset=iso-8859-1");         printwriter out = response.getwriter();          java.sql.connection con = null;         java.sql.preparedstatement pst = null;         resultset rs = null;          string url = "jdbc:mysql://localhost:3306/masters";           string user = "christine";         string password = "password";          try{             string exname = request.getparameter("exname");             con = drivermanager.getconnection(url, user, password);               pst = con.preparestatement("select * exercise exercise_name ='"+exname+"';");             rs = pst.executequery();                while (rs.next()){                 string name = rs.getstring("description_txt");                  out.print(exname);                 out.print(name);             }            }catch(sqlexception ex){         }finally {             try {                 if (rs != null){                     rs.close();                 }                 if (pst != null){                     pst.close();                 }                 if (con != null){                     con.close();                 }             }catch(sqlexception ex){             }                    }            } 

there few things need change in code.

as @balusc mentioned not suppress exception. write exception browser using out.println(ex); or throw new servletexception(ex). in finding cause of problem.

add class.forname("com.mysql.jdbc.driver"); before drivermanager.getconnection().

as using preparedstatement use

pst = con.preparestatement("select * exercise exercise_name =?"); pst.setstring(1, exname); rs = pst.executequery(); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -