eclipse jsp mysql_jsp eclipse 如何连接mysql

展开全部

这是表单页面代码!

图书管理

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();

}

}

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
采用Eclipse 编写的关于省市县三级菜单采用JSP +servlet+MYSQL ,数据库表的数据文件在src文件夹中 mysql> show tables; +----------------+ | Tables_in_area | +----------------+ | area | | cities | | provinces | +----------------+ 3 rows in set (0.00 sec) mysql> desc provinces; +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | pid | char(6) | NO | | NULL | | | province | varchar(100) | NO | | | | +----------+--------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> desc cities; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | cid | char(6) | NO | | NULL | | | city | varchar(40) | NO | | NULL | | | pid | char(6) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> desc area; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | aid | char(6) | NO | | NULL | | | area | varchar(40) | NO | | NULL | | | cid | char(6) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值