Pol*_*Dog 1 java mysql database jdbc
我正在做一个大学项目,我们必须构建一个功能齐全的 Java 应用程序,并配有使用 JDBC 的基本用户登录系统。
当通过 localhost 在 XAMPP 的 MariaDB MySQL 分支上使用时,基本用户登录系统工作正常。
显然,该系统无法在该特定网络之外工作,因此我四处寻找允许我始终在线托管 MySQL 数据库的方法,因此无论我去哪里,这个基本的用户登录系统都可以工作并且可以显示给任何有兴趣的人。
然后我找到了 db4free.net,到目前为止一切看起来都正常 - PHPMyAdmin 工作正常,并且我成功地使用用户名和密码从 localhost 版本导出和导入数据库到 db4free 的版本。
但我在如何实际将我的应用程序连接到 db4free 的系统以便登录系统按预期工作时遇到了麻烦。
这是处理连接的“连接模块”Java 类:
package com.fixer.dal;
import java.sql.*;
public class Connection_Module {
public static Connection connector(){
java.sql.Connection cnx = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://db4free.net:3306/db_fixer";
String user = "myuser";
String password = "mypassword";
try {
Class.forName(driver);
cnx = DriverManager.getConnection(url, user, password);
return cnx;
} catch (Exception e) {
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是检查数据库以查看用户名和密码是否与记录的数据匹配的函数(如果匹配,它将关闭登录屏幕并打开“主”屏幕):
Connection cnx = null;
PreparedStatement pst = null;
ResultSet rs = null;
public void login(){
String sql = "select * from usertable where username=? and password=?";
try {
pst = cnx.prepareStatement(sql);
pst.setString(1, Screen_Login_Username_Field.getText());
pst.setString(2, Screen_Login_Password_Field.getText());
rs = pst.executeQuery();
if(rs.next()){
Screen_Main Main = new Screen_Main();
Main.setVisible(true);
this.dispose();
cnx.close();
}else{
JOptionPane.showMessageDialog(null, "Invalid user or password.");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
Run Code Online (Sandbox Code Playgroud)
这主要是我的问题。db4free.net 给了我一个主机(“db4free.net”)和一个端口(“3306”),但我不知道它们到底去哪里。我已经尝试了一些方法来让它根据这里的其他问题工作,但没有一个成功地将我连接到他们系统上的数据库。
我究竟做错了什么?
我刚刚在 db4free.com 中创建了一个数据库(第一次),我使用了:
MySQL 8.x JDBC 驱动程序(我使用的是 JDBC 驱动程序 8.0.11)。
网址是:
jdbc:mysql://db4free.net:3306/数据库名称
然而,在 MySQL 8.x 中,最好添加以下参数以避免处理大量警告:
jdbc:mysql://db4free.net:3306/数据库名称?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
它立即发挥了作用。您确认他们发送的电子邮件了吗?您的帐户活跃吗?您使用的是 JDBC 驱动程序 8.x 吗?
| 归档时间: |
|
| 查看次数: |
3441 次 |
| 最近记录: |