报的错误,没选折数据库?java.sql.SQLException: No database selected
该怎么选折呢?
package com.coco.one;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class TestJdbc {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306";
String user="root";
String password="cocoa";
Connection com = DriverManager.getConnection(url, user, password);
Statement x = com.createStatement();
String sql ="insert into student values(\"xiao\",121,2)";
x.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
}
------解决方案--------------------
String url ="jdbc:mysql://localhost:3306";
楼主的URL不完整,没有指出本机的数据库实例的名称
例如,如果实例叫mysql,
那么,rul为:jdbc:mysql://localhost:3306/mysql