【问题标题】:Issue with connecting JSP page to local MySQL database: wrong Database name将 JSP 页面连接到本地 MySQL 数据库的问题:错误的数据库名称
【发布时间】:2017-06-02 13:39:46
【问题描述】:

我的项目目前由一组本地托管的 JSP 页面组成,这些页面连接到 mySQL 数据库,这也是本地的。

我正在尝试验证使用数据库进入网站的用户。然而问题在于连接。

在验证JSP页面中,我有这段代码:

<%
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    String server = "jdbc:mysql://localhost:3306/";
    String database = "my_db";
    String user = "user";
    String pass = "password";

    try {


        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pass);
        dataSource.setServerName(server);
        dataSource.setDatabaseName(database);
        connection = dataSource.getConnection();

        //Class.forName("com.mysql.jdbc.Driver").newInstance();
        //connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/?user=user&password=password");

        //Class.forName("com.mysql.jdbc.Driver").newInstance();
        //connection = DriverManager.getConnection(server/*+database*/+"?user="+user+"&password="+pass);

        //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        //connection=DriverManager.getConnection(server/*+database*/,user,pass);

        if(!connection.isClosed()){
            //do stuff
        }
    }catch(Exception ex){
        out.println("Cannot connect to database.<br>"+ex);
    }finally{//Release Resources
        if(resultSet!=null){
            try{resultSet.close();}catch(SQLException sqlEx){}
            resultSet = null;
        }

        if(statement != null){
            try{statement.close();}catch(SQLException sqlEx){}
            statement = null;
        }

        if(!connection.isClosed()){
            try{connection.close();}catch(SQLException sqlEx){}
            connection = null;
        }
    }
%>

我尝试了各种连接方法,但似乎都不起作用。

第一个(Datasource,未发表评论)用java.lang.NullPointerException 击中了我。页面输出:http://prntscr.com/ff37x9

第二个(单个StringDriverManager 连接)连接,但无法识别数据库。但是,当我添加 my_db 时,它告诉我它没有找到任何名为“my_db”的数据库。

第三和第四与第二相同。

我不明白发生了什么故障。我应该注意其他事情吗?

编辑:仅此页面需要连接,因为站点的其余部分通过 Java 的 session 对象进行管理

感谢您的宝贵时间。

【问题讨论】:

  • 使用驱动程序管理器,您可以连接,对吗?您正在尝试连接 Wicht 数据库用户吗?
  • 我正在尝试连接到本地数据库。这是我从 MySQL Workbench 获得的 JDBC 连接字符串:jdbc:mysql://localhost:3306/?user=root
  • 但它不起作用;如果我尝试创建一个语句并执行它,它说它没有找到数据库。
  • 你为什么用'?'在 url 字符串处?
  • @ThemistoklisGkasios 你提到你有 NPE,你能把堆栈跟踪放在这里吗? NPE 被扔在哪个类?

标签: java mysql jsp


【解决方案1】:

通常通过 JNDI 为 JEE 应用程序配置数据源。

您可以查看this topic

如果你没有机会编写java代码或使用JNDI,而你只能使用JSP,请考虑至少使用JSTL Tag library for SQL。

您可以配置数据源并执行以下示例中的操作:

<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>

<html>
   <head>
      <title>JSTL sql:query Tag</title>
   </head>

   <body>
      <sql:setDataSource var = "snapshot" driver = "com.mysql.jdbc.Driver"
         url = "jdbc:mysql://localhost/TEST"
         user = "root"  password = "pass123"/>

         <sql:query dataSource = "${snapshot}" var = "result">
            SELECT * from Employees;
         </sql:query>

      <table border = "1" width = "100%">
         <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
         </tr>

         <c:forEach var = "row" items = "${result.rows}">
            <tr>
               <td> <c:out value = "${row.id}"/></td>
               <td> <c:out value = "${row.first}"/></td>
               <td> <c:out value = "${row.last}"/></td>
               <td> <c:out value = "${row.age}"/></td>
            </tr>
         </c:forEach>
      </table>

   </body>
</html> 

您唯一需要做的就是将 JSTL 标记库添加到您的应用程序类路径中,例如将JSTL jar 放入您拥有 MySQL 驱动程序 jar 的同一文件夹中。

【讨论】:

  • 我在这个项目中使用 Glassfish。这是 DataSource 在我的情况下不起作用的原因吗?
  • @ThemistoklisGkasios 不,Glassfish 不是问题。
【解决方案2】:

尝试这种方式直接在 URL 连接上获取与数据库的连接

String driver = "com.mysql.jdbc.Driver";
String connection = "jdbc:mysql://localhost:3306/YourDBName";
String user = "root";
String password = "root";
Class.forName(driver);
Connection con = DriverManager.getConnection(connection, user, password);

【讨论】:

  • 两件事:你确定数据库 my_db 存在吗?用户对该数据库有权限?
  • 在工作台中,我有these two connections,具有相同的密码和相同的用户(root)。
【解决方案3】:

非常感谢@Krismorte 的帮助。

可以在 Workbench 中输入SELECT DATABASES; 找到数据库名称。

我使用的架构与我需要访问的数据库的名称相同。

MySQL Workbench PrintScreen

这是'dip_db_schm'

在我的例子中,连接子句是:

connection=DriverManager. getConnection("jdbc:mysql://localhost:3306/dip_db_schm","user","password");

【讨论】:

    猜你喜欢
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多