gpt4 book ai didi

java - 如何将数据库中的元素检索到 JDBC 程序中

转载 作者:行者123 更新时间:2023-11-29 09:24:45 29 4
gpt4 key购买 nike

我尝试了一个基本程序来将数据从数据库表检索到java程序中。编译结束后,运行代码时出现异常。控制台中没有显示错误。显示异常消息

import java.sql.*;
public class class1 {
public static void main(String args[]){
String url = "jdbc:mysql://localhost:3306//orders";
String username = "root";
String password = "Luxan@22";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, username, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT CUST_NAME FROM CUSTOMERS");
System.out.println("List of Registered customers: ");
while(rs.next()){
System.out.println(rs.getString("cust_name"));
}
st.close();
con.close();
}
catch(Exception e){
System.out.println("An Exception error occured while processing JDBC programme");
}
}
}

下面是我的控制台窗口中的输出

Fri Jan 17 19:34:24 IST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

An Exception error occured while processing JDBC programme

最佳答案

问题出在您的网址上。替换

"jdbc:mysql://localhost:3306//orders"

"jdbc:mysql://localhost:3306/orders"

请注意,我在 orders 之前删除了一个 /

关于java - 如何将数据库中的元素检索到 JDBC 程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59789018/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com