Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

got problem of connecting to mysql database using JDBC

843859Apr 13 2006 — edited Apr 18 2006
I put this post on section 'Enterprise & Distributed Computing > Web Applications > JavaServer Pages (JSP) and JSTL > ' already, and I just fount out this section, so I think I probably better to put it in here

hi, I got problem of connecting to mysql database using JDBC,I list what I did step by step and hoping you can tell me whether I did anything wrongly:

1. I have a jsp project called test4, I copy file 'mysql-connector-java-3.1.6-bin.jar' to folder 'test4\WEB-INF\lib'

2. export the whole project in war format: test4.war

3. upload it to live server

4. restart tomcat

at this point, I got ' java.lang.NullPointerException ' when try to connect the database.

5. I notice in the live server, in file 'tomcat4/conf/catalina.policy', I saw a example of granting the permission to a JDBC driver, so I adding the following in file 'catalina.policy':

grant codeBase "file:${catalina.home}/webapps/test4/WEB-INF/lib/mysql-connector-java-3.1.6-bin.jar"{
permission java.net.SocketPermission "localhost:3306", "connect";
permission java.net.SocketPermission "127.0.0.1:3306", "connect";
};

but I still got the same exception 'java.lang.NullPointerException', I wonder if I did that wrong in policy file or anyother reason? I can't anything wrong in my code as it works perfectly in my machine, so it must be something else, I think the username and password should be correct.

Many thanks for your help!

In my development machine, I got no problem at all, in live server, the user name and password also right, as when I entering the live server login as root, I can do commmand 'mysql -urui -pchangeme Spottheballtv' with no problem, so I wonder how do I get more information to solve this problem?

exception 'java.lang.NullPointerException' doesn't give me too much helpful information for debugging, so I wonder what else Exception I need to catch in order to get more useful information?

Many thanks for your help again!

Ray

============================
my code is very simple:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="payment.DbConnect" %>


<%
//out.println((DataSource)applicationEnv.lookup("jdbc/user"));
try{
java.sql.Connection con = null;
Statement stmt = null;
con=DbConnect.connectToDb("localhost", "Spottheballtv","rui","changeme");
stmt = con.createStatement();
//String query = "SELECT * FROM Ticket";
//ResultSet rs = stmt.executeQuery(query);
out.println("ok!");
}
catch (Exception ex)
{
out.println("1?!<br>");
out.println(ex);
ex.printStackTrace();
}
%>

my connection class:
package payment;

import java.sql.*;

public class DbConnect {
static String driverName = "com.mysql.jdbc.Driver";
static String dbUrl = "jdbc:mysql://";

public DbConnect() {
}


public static java.sql.Connection connectToDb(String hostName,String databaseName, String uid, String pw) throws Exception {
Connection conn = null;
String connName = dbUrl+hostName+":3306"+"/"+databaseName;
try{
Class.forName(driverName).newInstance();
conn = DriverManager.getConnection(connName+"?useUnicode=true&characterEncoding=GBK",uid,pw);
} catch (Exception e){ e.printStackTrace();}
return conn;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 16 2006
Added on Apr 13 2006
5 comments
280 views