• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

urgent, please help!got problem of connecting to mysql database using JDBC

R

raybristol

Guest
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}/psa-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;
}
}
 
Does your /etc/my.cnf have "skip-networking" in the configuration file? JDBC doesn't leverage UNIX sockets, so it has to use TCP/IP. The default installation of MySQL is to NOT use the TCP/IP socket (3306).
 
Hi, thanks you so sooooooooooo much for your reply!!!!!!!!

I will check it soon and let you know how it goes, many many thanks again!!!!!!!
 
Back
Top