数据库已连接但未检索数据

huangapple 未分类评论48阅读模式
英文:

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(&quot;com.mysql.jdbc.Driver&quot;);
    
    			con = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/appointment&quot;, &quot;root&quot;, &quot;&quot;);
    			System.out.println(&quot;Connection Successful&quot;);
    		} catch (Exception e) {
    			System.out.println(&quot;Connection unsuccessful&quot;);
    			System.out.println(&quot;&quot; + 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 = &quot;&quot;;
	try {
		Connection con = config.connect();
		if (con == null) {
			return &quot;Error while connecting to the database for reading appoinment details.&quot;;
		}
		output = &quot;&lt;table border=\&quot;1\&quot;&gt;&lt;tr&gt;&lt;th&gt;Appointment Id&lt;/th&gt;&lt;th&gt;Appointment Date&lt;/th&gt;&lt;th&gt;Appointment Venue&lt;/th&gt;&lt;th&gt;Doctor Assign&lt;/th&gt;&lt;th&gt;Patient Id&lt;/th&gt;&quot;
				+ &quot;&lt;th&gt;Actions&lt;/th&gt;&quot;;
		String query = &quot;select * from appointment&quot;;
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery(query);

		while (rs.next()) {
			String appId = Integer.toString(rs.getInt(&quot;app_Id&quot;));
			String appDate = rs.getString(&quot;app_Date&quot;);
			String appVenue = rs.getString(&quot;app_Venue&quot;);
			String docId = Integer.toString(rs.getInt(&quot;app_Doctor_Id&quot;));
			String patientId = Integer.toString(rs.getInt(&quot;app_Patient_Id&quot;));

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 = &quot;&quot;;
        try {
            Connection con = Config.connect(); // This should properly called
            if (con == null) {
                return &quot;Error while connecting to the database&quot;;
            }
}

huangapple
  • 本文由 发表于 2020年5月5日 21:06:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/61613901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定