我碰壁了,没有帮助我无法通过。
我正在尝试使用 Java Servlet 为我的 Android 应用程序做一个简单的登录和注册,问题是当我在 Tomcat/Eclipse 上本地运行它时一切正常但是当我在 Heroku 上免费部署 WAR 文件时服务器,数据库部分似乎根本不起作用。
我怀疑这实际上是驱动程序的问题,但我无法获取日志,因为我通过我的 Android 应用程序连接到它。
我在 freesqldatabase 上部署了我的数据库。我将我的 sql 驱动程序放在 tomcat 目录中并检查了一千次它在 Tomcat 库中。很确定 JDBC 驱动程序在部署时会导致问题,但我不明白为什么,应用程序的 android 部分一切正常。
这是servlet的代码:
Connection connection = null;
boolean check = false;
//this part does not work online
try {
Class.forName("driver");
connection = DriverManager.getConnection("jdbc:mysql://sql7.freesqldatabase.com/sql7112115", "sql7112115", "xxxxxxxxxxx");
response.getWriter().append("4");
String query = "select * from userdata where username = ? and pass = ? ";
response.getWriter().append("5");
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, "Sue");
statement.setString(2, "Pass1");
ResultSet result = statement.executeQuery();
check = result.next();
connection.close();
} catch (ClassNotFoundException e) {
System.out.print("Driver error");
} catch (SQLException e) {
System.out.print("SQL error");
}
if (check) {
response.getWriter().append("Login succesful");
} else {
response.getWriter().append("Login unsuccesful");
}
}
最佳答案
我首先注意到的是 Class.forName("driver")
。通常您需要规范的类名 - 类似于 com.mysql.jdbc.Driver
。
关于java - 带数据库的 Servlet 在 Eclipse 中工作正常但在服务器上崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36241231/