|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
java servlet with database connection
all right I am trying very unsuccessfully to create a java servlet that will take information from a form html page and insert it into an access database and then retreive that data and post it for the user to see. dbResult = dbStatement.executeUpdate(query); keeps saying that there is a type mismatch cannot convert from int to ResultSet. if anyone could please give me some clues as to what is wrong as well if there is anything else wrong with the program I would be eternally grateful. Thanks.
Code:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class DatabaseServlet extends HttpServlet
{
Connection dbConnect = null;
Statement dbStatement = null;
ResultSet dbResult = null;
String query;
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String id = request.getParameter("id");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String exam1 = request.getParameter("exam1");
String exam2 = request.getParameter("exam2");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Found a driver");
dbConnect = Driver
Manager.getConnection("jdbc:odbc:student", "", "");
System.out.println("Made a connection");
dbStatement = dbConnect.createStatement();
query = "INSERT INTO Student (StudentID, FirstName, LastName, Exam1, Exam2) " +
"VALUES (id, fname, lname, exam1, exam2)";
dbResult = dbStatement.executeUpdate(query);
dbStatement = dbConnect.createStatement();
query = "SELECT * FROM Student";
dbResult = dbStatement.executeQuery(query);
StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = dbResult.getMetaData();
int numberOfColumns = metaData.getColumnCount();
for(int i = 1; i <= numberOfColumns; i++)
{
results.append(metaData.getColumnName(i) + "\t");
}
results.append("\n");
while(dbResult.next())
{
for(int i = 1; i <= numberOfColumns; i++)
{
results.append(dbResult.getObject(i) + "\t");
}
results.append("\n");
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// send xhtml page to client
out.println("<?xml version=\"1.0\"?>");
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
"XHTML 1.0 Strict //EN\" \"http://www.w3.org" +
"/TR/xhtml1/DTD/xhmtl1-strict.dtd\">");
out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");
//head section of document
out.println("<head>");
out.println("<title>Student Information</title>");
out.println("</head>");
//body of document
out.println("<body>");
out.println("<p>Welcome to the Student Database! </p><br/>");
out.println("<p>" + results.toString() + "</p>");
out.println("<p><a href=\"/servlets-examples/servlet/ds\">Click here to enter more students</a></p>");
out.println("</body>");
//end xhtml document
out.println("</html>");
out.close();
dbStatement.close();
dbConnect.close();
System.out.println("Closed statement and connection");
}
}
|
|
#2
|
||||
|
||||
|
You write
query = "INSERT INTO Student (StudentID, FirstName, LastName, Exam1, Exam2) " + "VALUES (id, fname, lname, exam1, exam2)"; dbResult = dbStatement.executeUpdate(query); Now id is a string String id = request.getParameter("id"); I can be wrong, but you should check this : in most cases, ID is an int. that could be the cause of the error. And by the way : you're writing HTML code in your servlet. That's not wrong, but it's easier to use JSP's and servlets. Let the JSP write the HTML and let the servlets be the controller of the application. You can use the MVC model 2 for this. More information can be found at www.coreservlets.com , the web site from Marty Hall. He has written a very good book "core servlets and java server pages". It's in 2nd edition and the first edition can be downloaded for free in pdf format at his web site. |
|
#3
|
|||
|
|||
|
you change this String id = request.getParameter("id"); into Integer id=new Integer(request.getParameter("id"));
this will work.you are getting the error because in the database you would have stored it has a integer and here you are giving it has a string.check it out it will work. Quote:
|
|
#4
|
|||
|
|||
|
Hi
Actually, the problem is with the method. U've to use executeQuery() instead. executeUpdate() wont work here. Thats why that error is coming. Do let me know what happened. LG |
|
#5
|
|||
|
|||
|
Quote:
hi, he is using insert query. so have to use executeUpdate only... so for that you can use like this.. int t = st.executeUpdate("insert ...."); variable 't' will contain the no. of rows inserted successfully. Regards, Rk |
|
#6
|
|||
|
|||
|
query = "INSERT INTO Student (StudentID, FirstName, LastName, Exam1, Exam2) " +
"VALUES (id, fname, lname, exam1, exam2)"; dbResult = dbStatement.executeUpdate(query); ------------------------------------------------- The problem is that while you are trying to insert a row which will return the number of rows that were inserted as an integer value (which will be zero if nothing was inserted), you are trying to store that as a resultset which is not correct. The execute statement needs to be: int result = dbStatement.executeUpdate(query); Hope this works! |
|
#7
|
|||
|
|||
|
Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > java servlet with database connection |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|