Configuring web host mysql database in jsp website made in Netbeans

痴心易碎 提交于 2020-01-07 03:10:29

问题


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.



来源:https://stackoverflow.com/questions/32859795/configuring-web-host-mysql-database-in-jsp-website-made-in-netbeans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!