|
Oracle JSP 乱码
1.数据库
--删除表tushumingxi
drop table tushumingxi;
--创建表
create table tushumingxi
(
Shu_name varchar(50)
);
--提交
commit;
3.写一个数ju库连接Delei
package com.hbut.onlineShopping;
import java.sql.*;
public class DataBaseConnection {
......
private Connection conn = null ;
public DataBaseConnection() {
//构造方法
.....
}
public Connection getConnection() {
return this.conn ;
}
...关闭数据库
}
2.用JavaBean 寫一个Booklei
package com.hbut.onlineShopping;
import java.sql.Date;
public class Book {
private String shu_name;
public String getShu_name() {
return shu_name;
}
public void setShu_name(String shu_name) {
this.shu_name = shu_name;
}
}
3.声明一geBookDAO接口,确定fang法的名字2
package com.hbut.onlineShopping;
import java.util.*;
public interface BookDAO {
//添加书
public void insertIntoBook(Book book) throws Exception;
}
4.BookDAOImpl实现这個接口
public class BookDAOImpl implements BookDAO {
public void insertIntoBook(Book book) throws Exception {
String sql = "insert into tushumingxi (Shu_name) values (?)";
PreparedStatement pstmt = null;
DataBaseConnection dbc = null;
dbc = new DataBaseConnection();
try {
pstmt = dbc.getConnection().prepareStatement(sql);
pstmt.setString(1, book.getShu_name());
pstmt.executeUpdate();
if(pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dbc.close();
}
}
5.DAOFactory
package com.hbut.onlineShopping;
public class DAOFactory {
public static BookDAO getBookDAOInstance()
{
return new BookDAOImpl() ;
}
}
6.xie一个显示的insertBook.jsp,form表单传递到下一个JSP
<%@ page language="java" contentType="text/HTML; charset=GB18030"
pageEncoding="GB18030"%>
<%
request.setCharacterEncoding("gb2312");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>添加圖书</title>
</head>
<body>
<table>
<form action="insertToBook.jsp" method="post">
<tr>
<td>图书名稱:<input type="text" name="shu_name"><br></td>
</tr>
<tr>
<td><input type="submit" value="确定"></td>
</tr>
</form>
</table>
</body>
</html>
7.insertToBook.jspyonguseBeande到参数
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.hbut.onlineShopping.*" %>
<jsp:useBean id="book" scope="page" class="com.hbut.onlineShopping.Book"/>
<jsp:setProperty name="book" property="*"/>
<%
request.setCharacterEncoding("gb2312");
%>
<%
System.out.println(book.getShu_name());
%>
<%
DAOFactory.getBookDAOInstance().insertIntoBook(book);
out.print("<script>alert('添加图shu成功!');window.history.go(-1);</script>");
%>
8.结果显示
為????亂码
歡迎来到Java学xiZhe論坛,转载请注明地址:http://www.javaxxz.com. |
|