我对java非常陌生,我很难连接数据库。 谢谢你的帮助
我尝试了 derby,但我的数据库位于使用 xampp 的 mysql 中。
Statement stmnt;
ResultSet rs;
try{
String host = "jdbc:derby://localhost:3306/population";
String uName= "root";
String uPass="root";
Connection con = DriverManager.getConnection(host, uName, uPass);
//execute some sql and load into the result set
stmnt = con.createStatement();
String sql = "SELECT * FROM users_admin";
rs = stmnt.executeQuery(sql);
//move the cursor the first record and get the data
rs.next();
int id = rs.getInt("admin_id");
String id_admin = Integer.toString(id);
String username=rs.getString("username");
String pwd = rs.getString("password");
ta_user.setText(username);
ta_pwd.setText(pwd);
ta_id.setText(id_admin);
}
catch( SQLException err ){
System.out.println( err.getMessage());
}
我已经尝试过,在我的其他示例项目中编写代码,它显示错误:
java.net.ConnectException:连接到端口 3306 上的服务器本地主机时出错,并显示消息“连接被拒绝:连接”。
最佳答案
好消息!您不需要 Class.forName
并且您的 JDBC 驱动程序已成功加载。现在您需要一个 mysql 服务器或 derby 服务器,您在问题中使用两个不同的 jdbc url,监听端口 3306 进行连接。
关于java - 如果我有 jdbc :mysql://localhost:3306,我会在 Class.forName 中写什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26924523/