创建数据库实例
CreateDatabase.java
import java.sql.*;
public class CreateDatabase {
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_UR="jdbc:mysql://localhost/";
static final String USER="root";
static final String PWD="password";
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("连接到数据库");
conn=DriverManager.getConnection(DB_UR,USER,PWD);
System.out.println("创建一个数据库");
stmt= conn.createStatement();
String sql="CREATE DATABASE jdbc_db";
stmt.executeUpdate(sql);
System.out.println("数据库成功的创建完毕");
}catch(SQLException se){
se.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(stmt!=null){
stmt.close();
}
}catch(SQLException se){
se.printStackTrace();
}
try{
if(conn!=null){
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
System.out.println("GOODBye");
}
}