使用纯jdbc创建一个中文数据库,创建成功后连接到该中文数据库并建立中文表、中文字段 。提示Unknown database 。求教:::::::::::
错误:
代码:
package com.tq.platform.test;
import java.sql.*;
public class Test {
// JDBC驱动 和 URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";
// 数据库连接认证
static final String USER = "root";
static final String PASS = "123456";
@SuppressWarnings("resource")
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
String sql = "";
try {
Class.forName(JDBC_DRIVER);
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating database...");
stmt = conn.createStatement();
sql = "CREATE DATABASE `测试数据库` character set utf8";
stmt.executeUpdate(sql);
System.out.println("Database created successfully...");
String test_URL = "jdbc:mysql://localhost:3306/`测试数据库`?useUnicode=true&characterEncoding=utf-8";
conn = DriverManager.getConnection(test_URL, USER, PASS);
sql = "CREATE table `表1`(`字段1` varchar(20),`字段2` int)";
stmt.executeUpdate(sql);
sql = "select * from `表1`";
stmt.executeQuery(sql);
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}