Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 16th, 2004, 01:19 PM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
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");
		
			
	}
	
	
}

Reply With Quote
  #2  
Old May 22nd, 2004, 07:42 AM
gertcuppens's Avatar
gertcuppens gertcuppens is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 gertcuppens User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 28 m 17 sec
Reputation Power: 15
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.

Reply With Quote
  #3  
Old May 1st, 2005, 03:43 PM
nikhilbm nikhilbm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 1 nikhilbm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 49 sec
Reputation Power: 0
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:
Originally Posted by gallifray
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");
 
 
	}
 
 
}
 

Reply With Quote
  #4  
Old June 25th, 2007, 02:07 AM
slgayathri slgayathri is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 1 slgayathri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 39 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old June 28th, 2007, 01:13 AM
Ashvametra Ashvametra is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 2 Ashvametra User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m
Reputation Power: 0
Quote:
Originally Posted by slgayathri
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


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

Reply With Quote
  #6  
Old April 18th, 2008, 12:31 AM
rahul_in rahul_in is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 2 rahul_in User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 30 sec
Reputation Power: 0
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!

Reply With Quote
  #7  
Old April 21st, 2008, 01:12 AM
Annie79 Annie79 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Location: Beaverton OR
Posts: 17 Annie79 User rank is Just a Lowly Private (1 - 20 Reputation Level)  Folding Points: 49426 Folding Title: Beginner FolderFolding Points: 49426 Folding Title: Beginner FolderFolding Points: 49426 Folding Title: Beginner Folder
Time spent in forums: 7 h 25 m 11 sec
Reputation Power: 0
Quote:
Originally Posted by rahul_in
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!
Good catch, but this thread is 4 years old.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > java servlet with database connection


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap