嗨,你能分享一下异常的堆栈跟踪吗
尝试以下解决方案:
- 确保 MySQL Conncetor for java 包含在您的类路径中。
- 试试这个示例代码:
声明类变量:
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://ip_address:port_name/db_name";
static final String USER = "username";
static final String PASS = "password";
创建数据库连接的示例代码:
Connection conn = null;
Statement stmt = null;
// STEP 1: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 2: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 3: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM table_name";
ResultSet rs = stmt.executeQuery(sql);
然后迭代 rs 以获取行