Java / MySQL: Inserting row into table from Java Web Application (TomCat v7.0) -


basically, have 2 classes in web application, 1 class 'formprocess.java', takes information inserting html form, performs few calculations, outputs result html. works absolutely fine.

i have class adding data new row of mysql database, class called 'sqlconnect.java'. works fine if run seperately on server (ie. correctly inserts row on table).

what struggling getting sqlconnect methods run formprocess class. when add following formprocess:

private sqlconnect sql; 

and in method:

sql.dopost(request, response); 

i following error:

java.lang.nullpointerexception crunch.formprocess.doget(formprocess.java:27) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:728) 

this sqlconnect class @ moment (i left out imports etc.):

@webservlet(name="sql",urlpatterns={"/sql"}) public class sqlconnect extends javax.servlet.http.httpservlet {      formprocess process = new formprocess();      private static final long serialversionuid = 1l;      public sqlconnect() {      }      public void doget(httpservletrequest request,             httpservletresponse response) throws servletexception, ioexception {         performtask(request, response);     }      public void dopost(httpservletrequest request,             httpservletresponse response) throws servletexception, ioexception {         performtask(request, response);     }      public void performtask(httpservletrequest request,             httpservletresponse response) throws servletexception, ioexception {         testjndidatasource();     }      public void testjndidatasource() {         connection conn = null;         statement st = null;         resultset rs = null;         try {             initialcontext ctx = new initialcontext();             datasource ds = (datasource) ctx.lookup("java:comp/env/jdbc/crunchdb");              conn = ds.getconnection();              st = conn.createstatement();             st.executeupdate("insert log " + "values (20, '1', '1', " + process.salaryint + ", "+ process.takehomepaye +", "+ process.takehomecontractor +", 2)");           } catch (exception ex) {             ex.printstacktrace();         } {             try { if (rs != null) rs.close(); } catch (sqlexception e) { e.printstacktrace(); }             try { if (st != null) st.close(); } catch (sqlexception e) { e.printstacktrace(); }             try { if (conn != null) conn.close(); } catch (sqlexception e) { e.printstacktrace(); }         }     }  } 

i'm sure there simple way of doing can't work out, said sqlconnect class run fine on it's own.

cheers

unfortunately, class structure have right flawed. sqlconnect should not subclass of httpservlet, because people should never calling directly though webpage. instead, formprocess , other actual servlets should access sqlconnect.

so now, code should this:

public void testjndidatasource(int salaryint, double takehomepaye, string takehomecontractor) {     connection conn = null;     statement st = null;     resultset rs = null;     try {         initialcontext ctx = new initialcontext();         datasource ds = (datasource) ctx.lookup("java:comp/env/jdbc/crunchdb");          conn = ds.getconnection();          st = conn.createstatement();         st.executeupdate("insert log " + "values (20, '1', '1', " + process.salaryint + ", "+ process.takehomepaye +", "+ process.takehomecontractor +", 2)");       } catch (exception ex) {         ex.printstacktrace();     } {         try { if (rs != null) rs.close(); } catch (sqlexception e) { e.printstacktrace(); }         try { if (st != null) st.close(); } catch (sqlexception e) { e.printstacktrace(); }         try { if (conn != null) conn.close(); } catch (sqlexception e) { e.printstacktrace(); }     } } 

then, in formprocess:

public void performtask(httpservletrequest request,         httpservletresponse response) throws servletexception, ioexception {     int salaryint = //extract form...      double takehomepaye = //extract form...      string takehomecontractor = //extract form...      sqlconnect sqlconnect = new sqlconnect();     sqlconnect.testjndidatasource(salaryint, takehomepaye, takehomecontractor); } 

this more logical approach modeling how site should work. note: query right vulnerable sql injection. change statement preparedstatement per instructions here: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -