读取mysql中的中文数据问题

aboutagirl 2003-07-11 07:20:16
如果getConnection("jdbc:mysql://localhost/classmates","root",""),读出mysql中的中文数据显示为????,

如果改为getConnection("jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1")
则提示
java.sql.SQLException: General error: Incorrect database name 'mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1'

为什么?
...全文
28 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
aboutagirl 2003-07-11
  • 打赏
  • 举报
回复
源代码:
import java.sql.*;

public class db2
{
static public void main(String args[])
{
Connection conn = null;
Statement s = null;
ResultSet rs = null;
try
{
System.out.println("Loading JDBC driver .....");
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
System.out.println("Loaded.");
try
{
System.out.println("\nConnecting to MySQL ......");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=8859_1","root","");
System.out.println("Connected.");

try
{
System.out.println("\nCreating statement......");
s = conn.createStatement();
System.out.println("Created.");
try
{
System.out.println("\nCreating resultset......");
rs = s.executeQuery("select * from user");
System.out.println("Created.");
while (rs.next())
{
System.out.println("\n " + rs.getString(2));
}
}catch (Exception exce)
{
System.out.println("Failed.");
exce.printStackTrace();
}
}catch (Exception exc)
{
System.out.println("Failed.");
exc.printStackTrace();
}
}catch (Exception ex)
{
System.out.println("Failed !");
ex.printStackTrace();
}
}catch (Exception e)
{
System.out.println("Failed .");
e.printStackTrace();
}
finally
{
if (conn != null)
{
if (s != null)
{
if (rs != null)
{
try
{
System.out.println("\nClosing resultset......");
rs.close();
System.out.println("Closed.");
}catch (Exception eR)
{
System.out.println("Failed.");
}
}
try
{
System.out.println("\nClosing statement......");
s.close();
System.out.println("Closed.");
}catch (Exception eS)
{
System.out.println("Failed.");
}
}
try
{
System.out.println("\nClosing connection .....");
conn.close();
System.out.println("Closed.");
}catch (Exception exception)
{
System.out.println("Failed !");
exception.printStackTrace();
}
}
}
}
};

try语句太多:)
aboutagirl 2003-07-11
  • 打赏
  • 举报
回复
可是我同时写的PHP脚本却可以正常访问,并显示中文
XKP 2003-07-11
  • 打赏
  • 举报
回复
怎么可能加不加都是报mysql表的错????
重启服务器试试看?
lynx1111 2003-07-11
  • 打赏
  • 举报
回复
mysql 的数据库经常出中文问题!
aboutagirl 2003-07-11
  • 打赏
  • 举报
回复
对,加不加都会报错
只有getConnection("jdbc:mysql://localhost/mysql","root","")才能连接上
XKP 2003-07-11
  • 打赏
  • 举报
回复
不加上mysql这个数据表名还会是这样?
aboutagirl 2003-07-11
  • 打赏
  • 举报
回复
java.sql.SQLException: General error: Unknown database 'mysql?useUnicode=true&characterEncoding=8859_1'

还是这个……
郁闷

PS:我用的是Apache Tomcat/4.1.18
XKP 2003-07-11
  • 打赏
  • 举报
回复
getConnection("jdbc:mysql://localhost/?useUnicode=true&characterEncoding=8859_1
","root","")

这样呢?

很奇怪啊,我这边都可以哦
aboutagirl 2003-07-11
  • 打赏
  • 举报
回复
不行……
一个类型的错误,只是变为
java.sql.SQLException: General error: Incorrect database name '?user=root&password=&useUnicode=true&characterEncoding=8859_1'
XKP 2003-07-11
  • 打赏
  • 举报
回复
jdbc:mysql://localhost/user=root&password=&useUnicode=true&characterEncoding=8859_1

这样看看能不能连

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧