在Netbeans中的jsp网站中配置Web主机mysql数据库 [英] Configuring web host mysql database in jsp website made in Netbeans

查看:137
本文介绍了在Netbeans中的jsp网站中配置Web主机mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在NetBeans中建立了一个JSP网站,该网站通过tomcat(使用访问数据库)在本地服务器上进行了测试和测试,并且运行良好.我的网络主机已经为我提供了主机,数据库名称,用户名和密码.我想配置我的网站以使用此数据库.但是我不知道该怎么做.我在 web-inf/config 文件夹中看到了 system.properties 文件,其内容如下:

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

但是我很困惑如何修改此文件.另外,只能从Web主机访问该数据库.

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;
    }

推荐答案

我终于知道了.在函数" getProperties()"上方的java代码中,从" web-inf/config "文件夹中获取" system.properties "文件.可以在" system.properties "文件中注意到,该驱动程序用于建立odbc连接.但是我的是MySQL数据库,因此我们必须将驱动程序替换为" com.mysql.jdbc.Driver ".网址将更改为" jdbc:mysql://192.168.0.1:3306/",其中192.168.0.1是主机,3306是端口.在dbname字段中添加数据库名称,在duser字段中添加用户名,在dpass字段中添加密码.保存并重新部署该项目,它就可以连接了.

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.

这篇关于在Netbeans中的jsp网站中配置Web主机mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆