数据库问题
我的数据库创建脚本:(mysql数据库)
CREATE TABLE person
(
id varchar(20) not null primary key ,
name varchar(20) ,
password varchar(20)
) ;
INSERT INTO person VALUES ( 'LXH ', '李兴华 ', 'zzzzzz ') ;
连接数据库程序:
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<%!
String DBDRIVER = "com.mysql.jdbc.Driver " ;
String DBURL = "jdbc:mysql://localhost:3306/mldn " ;
String DBUSER = "root " ;
String DBPASSWORD = "123456 " ;
Connection conn = null ;
Statement pstmt = null ;
ResultSet rs = null ;
%>
<%
String sql= "SELECT * FROM person ";
try
{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
pstmt=conn.createStatement();
rs=pstmt.executeQuery(sql);
if(rs.next())
{
System.out.println(rs.getString( "id "));
System.out.println(rs.getString( "name "));
System.out.println(rs.getString( "password "));
}
rs.close();
pstmt.close();
conn.close();
}
catch(Exception e)
{
}
%>
输出结果为:LXH ?????? zzzzzz
怎么没得到name的值啊 具体原因是什么???
------解决方案--------------------是不是因为你的数据库不支持UTF8
------解决方案--------------------dbc:mysql://localhost:3306/mldn " ;后面指定编码
jdbc:mysql://localhost/databasename?useUnicode=true&characterEncoding=GB2312
------解决方案--------------------首先,在MySQL启动项即my.ini文件中指定字符集为gbk,然后在建立数据库和表的时候也要指定字符集为gbk,最后在URL中写入jdbc:mysql://localhost/databasename?useUnicode=true&characterEncoding=gbk,保证字符集从头到尾都一致!否则就会出现乱码!