展开全部
这是表单页面代码!
图书管理if(session.getAttribute("admin")==null){
response.sendRedirect("adminlogin.jsp");
}
else{
%>
图书添加
书 名: 作 者: 出版社: 类 型: Ifnew: 价 格: 图 片: 简 介: | ||
这是接受页面代码
无标题文档//String bookid=request.getParameter("book_id");
//int book_id=Integer.parseInt(bookid);
String book_name=request.getAttribute("book_name").toString();
//book_name=new String(book_name.getBytes("ISO-8859-1"),"GB2312");
String author=request.getAttribute("author").toString();
//author=new String(author.getBytes("ISO-8859-1"),"GB2312");
String publisher=request.getAttribute("publisher").toString();
//publisher=new String(publisher.getBytes("ISO-8859-1"),"GB2312");
String type=request.getAttribute("type").toString();
//type=new String(type.getBytes("ISO-8859-1"),"GB2312");
String ifNew=request.getAttribute("ifNew").toString();
String price=request.getAttribute("price").toString();
String img=request.getAttribute("fname").toString();
String jiejian=request.getAttribute("jiejian").toString();
//jiejian=new String(jiejian.getBytes("ISO-8859-1"),"GB2312");
String str[]={book_name,author,publisher,type,ifNew,price,img,jiejian};
String sql="insert into book values(?,?,?,?,?,?,?,?,getDate())";
DBMain.pexeUpdate(sql,str);
out.print("添加成功返回首页");
%>
这是数据库处理类
package bean;
import java.sql.*;
public class DBMain {
//连接数据库
public static Connection getcon(){
Connection con=null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=bookshop","sa","2601350");
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return con;
}
//用Statement查询数据库
public static ResultSet exeQuery(String sql){
ResultSet rs=null;
Connection con=getcon(); //Connection的对象
try {
Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stm.executeQuery(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return rs;
}
//用Statement类,执行添加,删除,更新数据库操作
public static void exeUpdate(String sql){
Connection con=getcon();
try {
Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
stm.executeUpdate(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
//用PreparedStatement类查询数据库
public static ResultSet pexeQuery(String sql,String[] str){
ResultSet rs=null;
Connection con=getcon();
try {
PreparedStatement pstm=con.prepareStatement(sql);
for(int i=0;i
pstm.setString(i+1,str[i]);
}
rs=pstm.executeQuery();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return rs;
}
//用PreparedStatement类实现添加,删除,更新数据库
public static void pexeUpdate(String sql,String[] str){
try {
Connection con=getcon();
PreparedStatement pstm=con.prepareStatement(sql);
for(int i=0;i
pstm.setString(i + 1,str[i]);
}
pstm.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起