Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   JDBC and Apache Tomcat pls help (http://www.programmingforums.org/showthread.php?t=11474)

paulchwd Oct 3rd, 2006 8:07 PM

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

Arevos Oct 4th, 2006 4:03 AM

JDBCTest is just a normal class; it needs to be a servlet tobe used with Tomcat.
:

  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.

jorrigal Oct 4th, 2006 4:55 AM

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.

paulchwd Oct 4th, 2006 7:39 AM

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

Arevos Oct 4th, 2006 10:44 AM

getObject ;)

paulchwd Oct 6th, 2006 9:53 AM

can i get a code exaple pls.. im kinda rusty on java..many thanks

paulchwd Oct 9th, 2006 8:11 PM

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 ?

Arevos Oct 10th, 2006 4:07 AM

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.

paulchwd Oct 10th, 2006 11:42 AM

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?

Arevos Oct 10th, 2006 12:01 PM

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.


All times are GMT -5. The time now is 1:17 AM.

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