Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to execute insertion!

 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is wrong with my project? ;-) ---> Unable to execute insertion!











MySQLQueryBrowser:
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the error you are getting?

Also you should not close the connection in the destroy method instead pu a finally blockand close the connection.

because the destroy method will be called only when the container decides to unload the servlet and will be called only once.

Also before closing the connection check whether the connection is not null and is not closed already.
 
David Kilcy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you told us what Exception you were getting.

But from glancing from your code it looks like your trying to do an insert on testo, which is your database. You need to do the insert on the table"new table" in your database.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing your question is about the database insertion. Two suggestions:

(1) Don't use a servlet to test your database logic. That's a really clumsy way of testing (as probably you are finding). Just write a small standalone piece of code which you can run to do the testing. Even better, put the database logic into a class which you can then include unchanged into your web project.

(2) Always use a PreparedStatement to do database updates. That ugly concatenation logic is easy to get wrong, especially when you have to put quotes around text fields and so on.

(3) Okay, three suggestions: always print the stack trace of the exception when you're catching an exception. Throwing it away and printing a generic message is a good way of not being able to find out what the problem was.
 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Kilcy wrote:It would help if you told us what Exception you were getting.

But from glancing from your code it looks like your trying to do an insert on testo, which is your database. You need to do the insert on the table"new table" in your database.



This is exception: Unable to execute insertion
catch (SQLException sqlEx)
{
out.println("<BR><CENTER><H2>Unable to execute insertion!</H2></CENTER>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
System.exit(1);
}


I have not Errors ;-)
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lenny Lee Petersen wrote:
David Kilcy wrote:It would help if you told us what Exception you were getting.

But from glancing from your code it looks like your trying to do an insert on testo, which is your database. You need to do the insert on the table"new table" in your database.



This is exception: Unable to execute insertion
catch (SQLException sqlEx)
{
out.println("<BR><CENTER><H2>Unable to execute insertion!</H2></CENTER>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
System.exit(1);
}


I have not Errors ;-)


Show us the stack trace.
eg. sqlEx.printStackTrace();
 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Show us the stack trace.
eg. sqlEx.printStackTrace();


Where can I see the stack trace? :-)
 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Description Resource Path Location Type
Classpath entry C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/mysql-connector-java-5.1.10-bin.jar will not be exported or published. Runtime ClassNotFoundExceptions may result. TheTest P/TheTest Classpath Dependency Validator Message
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this.
This will print the stack message in the browser.
You should also log your exception message somewhere before you display your own error message to the user.

So that it will be easy for you to debug.

out.println("
<CENTER><H2>"+sqlEx.printStackTrace()+"</H2></CENTER>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
System.exit(1);


Also look at David's message you are insert into Database instead of table.

 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Masilamani wrote:Try this.
This will print the stack message in the browser.
You should also log your exception message somewhere before you display your own error message to the user.

So that it will be easy for you to debug.

out.println("
<CENTER><H2>"+sqlEx.printStackTrace()+"</H2></CENTER>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
System.exit(1);


Also look at David's message you are insert into Database instead of table.



I get error when I try to put this code in servelt...+sqlEx.printStackTrace()+

I have changed to table...
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry sqlEx.printStackTrace() should be outside out.printlln statement.
 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Masilamani wrote:Sorry sqlEx.printStackTrace() should be outside out.printlln statement.


Thanks Masilamani



com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table VALUES('Petersen','Lenny','8943289')' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
at test.DbServlet.doPost(DbServlet.java:78)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try debug your code.

Print out your insert statement and make sure it is of correct syntax before do insertion.
 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now I have got the table in my eclipse ;-))



But surname is a Integer. How can I change this to String in mySql Query Browser?
If I do this I get a error..





Thanks...

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not worked much in MySQL but if you want to change the data type of an exisiting column make sure the column is null else you will get an error.

But the error you are getting is related to syntax error.(I could see some symbol in the query)

the query should should be Alter table tablename modify columnname datatype and make sure the column should be empty.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic