Friday, 15 March 2013

java - NullPointerException in JDBC -


This is JDBC code for customized connection. I am using my SQL 5 database server which is working properly. I have tried this code on net bean version 7.3. There are 3 classes

  Public class MyDbOperation {Private Static connection c; Private Static Statement St. Private Stable ResultSets Rs. Private stationary ArrayList al; Private Static Int M; Private station student student; Stable {try {class.forName ("com.mysql.jdbc.Driver"); C = DriverManager.getConnection ("jdbc: mysql: //127.0.0.1: 3306 / stp13", "root", "@ Ashish123"); Cents = c.createStatement (); } Hold (Exception E) {}} Public Fixed int getInsert throws (student) SQLException {m = st.executeUpdate ("Enter in student values" ("+ s.getRollNo () +", '"+ s.getFname ( ) + "',' '+ S.getLname () +' ',' '+ s.getEmail () +' ',' + s.getMob () + ',' + + s.getStreet () + '' , '' + S.getCity () + '') "); Return M;}}   

When I execute the code I get an error

 < Code> stp13 at java.lang.NullPointerException.MyDbOperation.getInsert (MyDbOperation.java:44) stp13.Stp13hbt.main (Stp13hbt.java:24)   

with the code that is wrong ? How to solve?

Your problem is here

  try {class .forName ("com.mysql.jdbc.Driver"); C = DriverManager.getConnection ("jdbc: mysql: //127.0.0.1: 3306 / stp13", "root", " @ Ashish123 "); cents = c.createStatement ();} hold (exception e) {}   

You do not restore or log in your exception, so it has been swallowed Change it to st then zero

  try {class.forName ("com.mysql.jdbc.Driver"); C = DriverManager.getConnection ("jdbc: mysql: //127.0.0.1: 3306 / stp13", "root", "@ Ashish123"); Cents = c.createStatement (); } Hold (exception e) {new ExceptionInInitializerEreror (e); }   

Your code is full of memory leaks and SQL injection opportunities. Please please please read the correct usage of JDBC.

No comments:

Post a Comment