英文:
Database is connecting but not retrieving data
问题
以下是翻译好的内容:
从数据库检索数据的简单程序。连接成功,但在控制台中出现以下错误:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 连接成功
配置文件
public class Config {
public Connection connect() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/appointment", "root", "");
System.out.println("连接成功");
} catch (Exception e) {
System.out.println("连接不成功");
System.out.println("" + e);
}
return con;
}
public static void main(String[] args) {
Config config = new Config();
Connection con = config.connect();
}
}
调用数据库连接
public String readAppoinment() {
String output = "";
try {
Connection con = config.connect();
if (con == null) {
return "连接到数据库以读取预约详细信息时出错。";
}
output = "<table border=\"1\"><tr><th>预约编号</th><th>预约日期</th><th>预约地点</th><th>指定医生</th><th>患者编号</th>"
+ "<th>操作</th>";
String query = "select * from appointment";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String appId = Integer.toString(rs.getInt("app_Id"));
String appDate = rs.getString("app_Date");
String appVenue = rs.getString("app_Venue");
String docId = Integer.toString(rs.getInt("app_Doctor_Id"));
String patientId = Integer.toString(rs.getInt("app_Patient_Id"));
在浏览器中看到结果为:
连接到数据库以读取预约详细信息时出错。
英文:
I am trying to create a simple data retrieving program from the database. The connection is Successful but i get this error in the console java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Connection Successful
config file
public class Config {
public Connection connect() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/appointment", "root", "");
System.out.println("Connection Successful");
} catch (Exception e) {
System.out.println("Connection unsuccessful");
System.out.println("" + e);
}
return con;
}
public static void main(String[] args) {
Config config = new Config();
Connection con = config.connect();
}
}
Calling the database connection
public String readAppoinment() {
String output = "";
try {
Connection con = config.connect();
if (con == null) {
return "Error while connecting to the database for reading appoinment details.";
}
output = "<table border=\"1\"><tr><th>Appointment Id</th><th>Appointment Date</th><th>Appointment Venue</th><th>Doctor Assign</th><th>Patient Id</th>"
+ "<th>Actions</th>";
String query = "select * from appointment";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String appId = Integer.toString(rs.getInt("app_Id"));
String appDate = rs.getString("app_Date");
String appVenue = rs.getString("app_Venue");
String docId = Integer.toString(rs.getInt("app_Doctor_Id"));
String patientId = Integer.toString(rs.getInt("app_Patient_Id"));
I am getting
Error while connecting to the database
As a result in the browser.
答案1
得分: 0
我相信问题出在这段代码上。假设下面的代码在不同的类中。
public String readAppoinment() {
String output = "";
try {
Connection con = Config.connect(); // 这个应该被正确调用
if (con == null) {
return "连接数据库时出错";
}
}
英文:
I believe the problem lies in this code. Assuming this below code is in different class.
public String readAppoinment() {
String output = "";
try {
Connection con = Config.connect(); // This should properly called
if (con == null) {
return "Error while connecting to the database";
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论