通过javaBean往数据库里插入中文信息不会出现乱码,但是提取到javabean输出到jsp页面就出现乱码数据库编码是GBK;jsp页面:
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="bbs.com.Posts" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>主页</title>
</head>
<jsp:useBean id="sqlB" scope="page" class="bbs.com.SqlBoard"/>
<body background = "1d.jpg">
<img src = "dd.gif">
<br><br>
<center><img src = "ys.jpg" width= 250 hight = 150></center> <a href="load/load.jsp">登陆</a>
<a href="load/register.jsp">注册</a>
<br>
<br>
<hr width = "85%" size = 4 align = center color = "#">
<br>
<table align="center" border = "3" bgcolor = "#e2f6cd" width = 80% rules = "rows">
<%
ArrayList<Posts> alist = null;
alist = sqlB.showPosts();//获得arraylist对象
%>
<tr>
<th>ID</th>
<th>标题</th>
<th>发帖时间</th>
<th>作者</th>
</tr>
<%
for(int i=0;i<alist.size();i++){
out.println("<tr>");
out.println("<td>");
out.println(alist.get(i).getId());//输出id
out.println("</td>");
out.println("<td>");
%>
<b> <a href = "postsManage/visiter.jsp?id=<%=alist.get(i).getId()%>"><%=alist.get(i).getTitle()%></a></b>
<%
out.println("</td>");
out.println("<td>");
out.println("<font size = 1 >");
out.println(alist.get(i).getDate());//输出发帖时间
out.println("</font>");
out.println("</td>");
out.println("<td>");
out.println("<font size = 1 >");
out.println(alist.get(i).getUserName());//获取作者
out.println("</font>");
out.println("</td>");
out.println("</tr>");
}
%>
</table>
</body>
</html>sqlBoard中showPosts方法:
public ArrayList<Posts> showPosts() throws SQLException, UnsupportedEncodingException{//显示所有会员名称
ArrayList<Posts> alist = new ArrayList<Posts>();
Connection conn = null; //数据连接
PreparedStatement pstmt = null;//数据库操作
ResultSet rs = null;
String sql = "select * from posts";
conn = sqlConnection();// 连接数据库
pstmt = conn.prepareStatement(sql);//实例化
rs = pstmt.executeQuery();
while(rs.next()){
Posts p = new Posts();
p.setId(rs.getInt("id"));//将帖子id添加
p.setTitle(rs.getString("title"));//将帖子标题添加
System.out.println(p.getTitle());
p.setUserName(rs.getString("userName"));//将帖子作者添加
p.setDate(rs.getDate("date").toString());//添加时间
p.setDetails(rs.getString("details"));//添加内容
alist.add(p);
}
rs.close();
pstmt.close();
conn.close();
return alist;
} javabean:
package bbs.com;import java.io.UnsupportedEncodingException;public class Posts {
private int id;
private String title;
private String userName;
private String details;
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDetails() throws UnsupportedEncodingException {
return this.details = new String(details.getBytes("ISO8859-1"),"GBK");
}
public void setDetails(String details) throws UnsupportedEncodingException {
this.details = new String(details.getBytes("ISO8859-1"),"GBK");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() throws UnsupportedEncodingException {
return this.title = new String(title.getBytes("ISO8859-1"),"GBK");
}
public void setTitle(String title) throws UnsupportedEncodingException {
this.title = new String(title.getBytes("ISO8859-1"),"GBK");
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}数据库javabeanString
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="bbs.com.Posts" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>主页</title>
</head>
<jsp:useBean id="sqlB" scope="page" class="bbs.com.SqlBoard"/>
<body background = "1d.jpg">
<img src = "dd.gif">
<br><br>
<center><img src = "ys.jpg" width= 250 hight = 150></center> <a href="load/load.jsp">登陆</a>
<a href="load/register.jsp">注册</a>
<br>
<br>
<hr width = "85%" size = 4 align = center color = "#">
<br>
<table align="center" border = "3" bgcolor = "#e2f6cd" width = 80% rules = "rows">
<%
ArrayList<Posts> alist = null;
alist = sqlB.showPosts();//获得arraylist对象
%>
<tr>
<th>ID</th>
<th>标题</th>
<th>发帖时间</th>
<th>作者</th>
</tr>
<%
for(int i=0;i<alist.size();i++){
out.println("<tr>");
out.println("<td>");
out.println(alist.get(i).getId());//输出id
out.println("</td>");
out.println("<td>");
%>
<b> <a href = "postsManage/visiter.jsp?id=<%=alist.get(i).getId()%>"><%=alist.get(i).getTitle()%></a></b>
<%
out.println("</td>");
out.println("<td>");
out.println("<font size = 1 >");
out.println(alist.get(i).getDate());//输出发帖时间
out.println("</font>");
out.println("</td>");
out.println("<td>");
out.println("<font size = 1 >");
out.println(alist.get(i).getUserName());//获取作者
out.println("</font>");
out.println("</td>");
out.println("</tr>");
}
%>
</table>
</body>
</html>sqlBoard中showPosts方法:
public ArrayList<Posts> showPosts() throws SQLException, UnsupportedEncodingException{//显示所有会员名称
ArrayList<Posts> alist = new ArrayList<Posts>();
Connection conn = null; //数据连接
PreparedStatement pstmt = null;//数据库操作
ResultSet rs = null;
String sql = "select * from posts";
conn = sqlConnection();// 连接数据库
pstmt = conn.prepareStatement(sql);//实例化
rs = pstmt.executeQuery();
while(rs.next()){
Posts p = new Posts();
p.setId(rs.getInt("id"));//将帖子id添加
p.setTitle(rs.getString("title"));//将帖子标题添加
System.out.println(p.getTitle());
p.setUserName(rs.getString("userName"));//将帖子作者添加
p.setDate(rs.getDate("date").toString());//添加时间
p.setDetails(rs.getString("details"));//添加内容
alist.add(p);
}
rs.close();
pstmt.close();
conn.close();
return alist;
} javabean:
package bbs.com;import java.io.UnsupportedEncodingException;public class Posts {
private int id;
private String title;
private String userName;
private String details;
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDetails() throws UnsupportedEncodingException {
return this.details = new String(details.getBytes("ISO8859-1"),"GBK");
}
public void setDetails(String details) throws UnsupportedEncodingException {
this.details = new String(details.getBytes("ISO8859-1"),"GBK");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() throws UnsupportedEncodingException {
return this.title = new String(title.getBytes("ISO8859-1"),"GBK");
}
public void setTitle(String title) throws UnsupportedEncodingException {
this.title = new String(title.getBytes("ISO8859-1"),"GBK");
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}数据库javabeanString
解决方案 »
- sitemesh只能用于jsp页面吗?,纯html的怎么办呢?
- Pattern,Matcher replace 替换字符串
- 用BufferedReader读取windows下好好的,切换到linux下就成乱码了???
- javaScript怎么取ID相同的值?
- 小弟请教一下数据字段显示的问题。
- apache运行问题?
- dwr怎么调用struts2中的action里的方法
- Cookie[] cookies=request.getCookies(); 是什么意思?
- 运用的是Hibernate,实体类用的是hibernate注解。这是哪里出了问题啦
- 关于business objects的desktop Intelligence读取
- web截图问题
- 项目中开发人员的责任和注意的要点?
将项目和数据库默认设为utf8乱码问题解决了
数据库转换utf8