使用纯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!");
}
}
我试了下 ,String test_URL = "jdbc:mysql://localhost:3306/测试数据库
?useUnicode=true&characterEncoding=utf-8"; 会报如下错误:java.sql.SQLSyntaxErrorException: Unknown database '???è????°?????“
',
改成 测试数据库 后错误消失了
Java:jdbc连接数据库插入中文数据乱码问题
String test_URL = "jdbc:mysql://localhost:3306/`测试数据库`?useUnicode=true&characterEncoding=utf-8";
---->改为
String test_URL = "jdbc:mysql://localhost:3306/测试数据库
?useUnicode=true&characterEncoding=utf-8";
试试看
右键项目 最后一个 改改编码格式 ,,,。。如果可以的话 可以配置一个过滤器
你的数据库名为什么是中文啊
。
数据库名改成用英文来命名试下
在url后面添加参数useUnicode=true&characterEncoding=utf-8确定使用什么编码就能搞定了
String test_URL = "jdbc:mysql://localhost:3306/测试数据库
?useUnicode=true&characterEncoding=utf-8";
这应该是创建数据库时候有问题,你看下数据库名字改成英文,在试试看
数据库和jdbc连接的字符集不一样,识别不了,乱码了
mysql的字符集设置utf8,在my.ini,改一下试试
"jdbc:mysql://localhost:3306/测试数据库?useUnicode=true&characterEncoding=utf-8"; 这样就可以了的,实在不行就修改mysql的字符集
两个都设置为UTF-8。就行了
1.将数据库连接配置改成这段:jdbc.url=jdbc:log4jdbc:mysql://127.0.0.1:3306/database?characterEncoding=utf8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false
2.数据库建议不要使用中文
链接数据库后加转化码
需要更改mysql的字符集为utf-8