Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 3rd, 2006, 7:07 PM   #1
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Exclamation JDBC and Apache Tomcat pls help

I have this simple code for my JDBC java file:




import java.sql.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;




public class JDBCTest
{
	 public static void main(String[] args) 
	 {
	

		 try
		 {
		 	Class.forName("com.mysql.jdbc.Driver").newInstance();
		
		 }
		 catch (Exception ex)
		 {
		 }

		 try
		 {
		 	    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/db1");

				Statement stmt =  conn.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT * FROM tb1");

				System.out.println(rs);


		 }
		 catch (SQLException ex)
		 {
		 }
		 


		 
	 }
}


I put the compiled code in the webapps/Root/web-inf/classes folder in the apache directory

heres my web.xml file (in the webapps/Root/web-inf folder)

<?xml version="1.0" encoding="ISO-8859-1" ?> 
- <!--   Copyright 2004 The Apache Software Foundation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  --> 
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <display-name>Welcome to Tomcat</display-name> 
  <description>Welcome to Tomcat</description> 
- <!--  JSPC servlet mappings start 
  --> 
- <servlet>
  <servlet-name>org.apache.jsp.index_jsp</servlet-name> 
  <servlet-class>org.apache.jsp.index_jsp</servlet-class> 
  <servlet-name>test</servlet-name> 
  <servlet-class>JDBCTest</servlet-class> 
  </servlet>
- <servlet-mapping>
  <servlet-name>org.apache.jsp.index_jsp</servlet-name> 
  <url-pattern>/index.jsp</url-pattern> 
  <servlet-name>test</servlet-name> 
  <url-pattern>/test.htm</url-pattern> 
  </servlet-mapping>
- <!--  JSPC servlet mappings end 
  --> 
  </web-app>

when i do: http://localhost:8080/test.htm

i get this:

HTTP Status 404 - /test.htm

--------------------------------------------------------------------------------

type Status report

message /test.htm

description The requested resource (/test.htm) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.17


any help is appreciated .... many many thanks
paulchwd is offline   Reply With Quote
Old Oct 4th, 2006, 3:03 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
JDBCTest is just a normal class; it needs to be a servlet tobe used with Tomcat.
java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import javax.servlet.http.*;
  3. import javax.servlet.*;
  4.  
  5. public class TestServlet extends HttpServlet
  6. {
  7. public void doGet (HttpServletRequest request, HttpServletResponse response)
  8. throws ServletException, IOException
  9. {
  10. PrintWriter out = response.getWriter();
  11. out.println("Hello world");
  12. }
  13. }
Google "java servlets" or "java servlets tutorial" for some further examples.
Arevos is offline   Reply With Quote
Old Oct 4th, 2006, 3:55 AM   #3
jorrigal
Newbie
 
Join Date: Oct 2006
Posts: 4
Rep Power: 0 jorrigal is on a distinguished road
If you want to retrieve data form table tb1, you should use rs.getXXX() functions like getInt, getString and etc.., ResultSet doesnot retrieves the data ,it just point the row.
jorrigal is offline   Reply With Quote
Old Oct 4th, 2006, 6:39 AM   #4
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
i shal try that... many thanks...
but is there a way to just get the data in the result set no mattter if its interger or string
paulchwd is offline   Reply With Quote
Old Oct 4th, 2006, 9:44 AM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
getObject
Arevos is offline   Reply With Quote
Old Oct 6th, 2006, 8:53 AM   #6
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Exclamation

can i get a code exaple pls.. im kinda rusty on java..many thanks
paulchwd is offline   Reply With Quote
Old Oct 9th, 2006, 7:11 PM   #7
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
I am using MYSql as a DBMS and the MYSql conector J as a driver... do any of you know what to put for the url ...

as in Connection conn = DriverManager.getConnection(" ");

the documentation wasnt much help...


and do i need a webserver to run JDBC code ?
paulchwd is offline   Reply With Quote
Old Oct 10th, 2006, 3:07 AM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
A typical value for the url would be "jdbc:mysql://localhost:3306/<database name>". Note that with MySQL, you have to GRANT appropriate priviledges to the user accessing the database otherwise you'll get a "Server configuration denies access to data source" error.

And no, you don't need a webserver to run JDBC code. You can run it straight from a normal "main()" application.
Arevos is offline   Reply With Quote
Old Oct 10th, 2006, 10:42 AM   #9
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Exclamation

icic..how do i grant privlidges?


also: to log into MYsql console i need a password, (the root password), that wouldnt be the password I use int he connection info is it?
paulchwd is offline   Reply With Quote
Old Oct 10th, 2006, 11:01 AM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
You need to read up on MySQL a little more, specifically how to create new users and to grant them priviledges. I won't go into it here, as there's 1001 articles online that'll explain it better than I could.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC