Configuring web host mysql database in jsp website

2019-09-01 02:31发布

问题:

I have made a JSP website in NetBeans which I tried and tested on my local server through tomcat(using access database) and it worked fine. My web host has provided me the host, database name, username and password of the database. I want to configure my website to use this database. But I don't know how to do that. I have seen the system.properties file in web-inf/config folder whose content are like this:

JNDI_NAME=java:com/env/Oracle/jndi
db.login=
db.password=
driver=sun.jdbc.odbc.JdbcOdbcDriver
url=jdbc:odbc:mydb
duser=
dpass=
logfile=log/aoc_log.txt
dbname=my_db

But I am confused how to modify this file. Also, the database is only accessible from the web host.

Below code shows how connection is made (I think so...)

public Connection getConnection() 
    {
        try
        {
            if(con==null)
            {
                try 
                {
                   Properties p = getProperties();
                   Class.forName(p.getProperty("driver"));
                   System.out.println("Driver loaded");
                   con = DriverManager.getConnection(p.getProperty("url"),p.getProperty("duser"),p.getProperty("dpass"));
                   System.out.println("Connection established");                     

                }
                catch (ClassNotFoundException cnf)
                {
                    LoggerManager.writeLogWarning(cnf);
                }
            }
        } 
        catch (SQLException sqlex) 
        {               
            sqlex.printStackTrace();
            LoggerManager.writeLogSevere(sqlex);
        }  
        return con;
    }

回答1:

I finally figured it out. In the java code above the function "getProperties()" fetch the 'system.properties' file from "web-inf/config" folder. It can be noticed in the 'system.properties' file that the driver used is for making an odbc connection. But mine is a MySQL database and hence we have to replace the driver with 'com.mysql.jdbc.Driver'. The url will be changed as 'jdbc:mysql://192.168.0.1:3306/' where 192.168.0.1 is the host and 3306 is the port. Add your database name in dbname field, username in duser field and password in dpass field. Save and redeploy the project and it gets connected.