使用JDBC连接Mysql的3种方式

来源:互联网 发布:mmd动作数据载入不动 编辑:程序博客网 时间:2024/05/02 13:49
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);}}



原创粉丝点击