用程序读出也无乱码
程序运行的结果:
插入中文成功!
1 liyaxi 30
2 李亚希 30
3 haoren 24
4 李天豪 24
5 李亚希 30
6 古龙 30
7 金庸 30
8 黄易 30
接下来 ,在web程序里插入读取 ,
一个注册页面:reg.jsp 页面提交到regsucc.jsp 页面插入数据成功后 , 将跳转到
show.jsp页面。
页面编码和过滤器都采用gbk编码。
详细代码我上传到附件里。
package liyaxi;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author 李亚希 天豪工作室 2009-9-12
*/
public class MysqlConn {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
try {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "791129");
Statement smt = conn.createStatement();
String sql = "insert into myuser(username, age , sex) values('黄易',30 ,'男')";
int a = 0;
a = smt.executeUpdate(sql);
if(a==1)
{
System.out.println("插入中文成功!");
}
else{
System.out.println("插入中文失败!");
}
ResultSet rs = smt.executeQuery("select *from myuser");
while (rs.next()) {
System.out.println(rs.getInt(1) + " \t " + rs.getString(2)
+ "\t" + rs.getString(3));
}
rs.close();
smt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
程序运行的结果:
插入中文成功!
1 liyaxi 30
2 李亚希 30
3 haoren 24
4 李天豪 24
5 李亚希 30
6 古龙 30
7 金庸 30
8 黄易 30
接下来 ,在web程序里插入读取 ,
一个注册页面:reg.jsp 页面提交到regsucc.jsp 页面插入数据成功后 , 将跳转到
show.jsp页面。
页面编码和过滤器都采用gbk编码。
详细代码我上传到附件里。