public class Test 01{
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection conn = DriverManager.getConnection(url, "root", "000000");
System.out.println(conn);
Statement stat = conn.createStatement();
String create="create table person(pid integer, name varchar(20))";
stat.execute(create);
}
}
当然还有另外一种连接方式:
public class Test01 {
public static void main(String[] args) throws Exception {
Driver driver=new com.mysql.jdbc.Driver();//与 Class.forName("com.mysql.jdbc.Driver")等价
String url="jdbc:mysql://localhost:3306/test";
Properties info=new Properties();
info.setProperty("user", "root");
info.setProperty("password", "000000");
Connection connection= driver.connect(url, info);
System.out.println(connection);
}
}
当然这种连接方式也是可以的:
public class Test01 {
public static void main(String[] args) throws Exception {
Driver driver=new com.mysql.jdbc.Driver();
String url="jdbc:mysql://localhost:3306/test";
Properties info=new Properties();
info.setProperty("user", "root");
info.setProperty("password", "000000");
DriverManager.registerDriver(driver);
Connection connection= DriverManager.getConnection(url, info);
System.out.println(connection);
}
}