使用JDBC创建数据库

使用JDBC不仅能连接操作数据库,甚至还能创建数据库,只需要权限允许的条件下,比如root。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class DBCreateTest {
public static String CONN_URL = "jdbc:mysql://localhost:3306";

public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DBCreateTest.CONN_URL, "root", "123456");
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public static void main(String[] args) {
String sql = "CREATE DATABASE if not exists `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
try {
Statement stmt = DBCreateTest.getConn().createStatement();
stmt.executeUpdate(sql);
stmt.close();
System.out.println("successed!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :