![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 139
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
JDBCTest is just a normal class; it needs to be a servlet tobe used with Tomcat.
java Syntax (Toggle Plain Text)
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2006
Posts: 4
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 139
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
getObject
![]() |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 139
Rep Power: 4
![]() |
can i get a code exaple pls.. im kinda rusty on java..many thanks
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 139
Rep Power: 4
![]() |
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 ? |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 139
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|