我正在学习Java,我遵循了一个教程,这是代码:
package simplejdbcapplication;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SimpleJDBCApplication {
static final String DB_URL ="jdbc:mysql://localhost:3306/testdb";
static final String DB_DRV = "com.mysql.jdbc.Driver";
static final String DB_USER = "mickey";
static final String DB_PASSWD = "mickey";
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWD);
statement=connection.createStatement();
resultSet=statement.executeQuery("SELECT * FROM books");
while (resultSet.next()) {
System.out.printf("%s\t%s\t%s\t%f\n",
resultSet.getString(1),
resultSet.getString(2),
resultSet.getString(3),
resultSet.getFloat(4));
}
} catch(SQLException ex){
System.out.println("SQLException: "+ex);
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException ex) {
System.out.println("SQLException: "+ex);
}
}
}
}
但是我抛出一个连接错误,我认为这是Xampp的某些配置,我留下了项目的图像:
非常感谢您的帮助。
PS:我留下了教程链接:https://www.developer.com/java/data/creating-a-jdbc-application-in-netbeans-a-step-by-step-guide.html