实验12 JDBC与JSP实践 下载本文

图12-12 导入db_ilike.mdb数据库(1)

图12-13 导入db_ilike.mdb数据库(2)

(2)在Myeclipse中新建Web项目,导入SQLserver对应的驱动程序包,如图12-14所示。

11

图12-14 导入SQL驱动程序包

(3)新建supplier_edit.jsp,使用小脚本加载对应的数据库驱动,注意驱动字符串的拼写,不同数据库的驱动字符串是不一样的,格式一般在数据库厂商的网站上可以查询得到,例如:SQLserver数据库2008的加载驱动的语句如下。

Class.forName(\);

使用DriverManager类的getConnection方法获取连接对象,getConnection方法的主要参数有数据库的URL访问地址,数据库名,访问数据库的账户名,登录数据库使用的密码。

Connection con = DriverManager.getConnection

(\2345;\);

利用得到的Connection对象生成Statement对象,查询数据库后的结果在JSP页面上显示,supplier_edit.jsp的完整代码如下。

源代码:

<%@ page contentType=\%> <%@ page import=\ %>

修改供应商信息

<%

long supplierID=1; try{

Class.forName(\);

Connection con =

DriverManager.getConnection(\ilike;user=sa;password=12345;\);

12

Statement dbc = con.createStatement();

String sqlStmt= \

supplierChargeName,supplierPhone,supplierEmail,supplierQQ,supplierRemark from supplier_tb where supplierID= \+supplierID;

%>

ResultSet rs= dbc.executeQuery(sqlStmt); rs.next();

String supplierName=rs.getString(1); String supplierAddress=rs.getString(2); String supplierChargeName=rs.getString(3); String supplierPhone =rs.getString(4); String supplierEmail =rs.getString(5); String supplierQQ =rs.getString(6); String supplierRemark=rs.getString(7); rs.close();

编辑供应商信息


\ />

13

名称:
联系人:
电话:
email:

\ size=\>

QQ:

\ size=\>

供应商地址:

\ size=\>

备 注:
<%

dbc.close();

}catch(Exception e1) {out.println(e1.toString());}

%>

14

(4)编写m_supplier_edit.jsp实现数据库信息修改功能。

源代码:

<%@ page contentType=\ %> <%@ page import=\ %>

<%request.setCharacterEncoding(\);%> <%

String supplier_id =

request.getParameter(\)==null?\:request.getParameter(\er_id\);

long fID = Long.parseLong(supplier_id); String supplier_name =

request.getParameter(\)==null?\:request.getParameter(\lier_name\);

String supplier_address =

request.getParameter(\)==null?\:request.getParameter(%upplier_address\);

String supplier_chargeName =

request.getParameter(\)==null?\:request.getParameter(\);

String supplier_phone =

request.getParameter(\)==null?\:request.getParameter(\plier_phone\);

String supplier_email =

request.getParameter(\)==null?\:request.getParameter(\plier_email\);

String supplier_QQ =

request.getParameter(\)==null?\:request.getParameter(\er_QQ\);

String supplier_remark =

request.getParameter(\)==null?\:request.getParameter(\pplier_remark\);

String sql=\

\\'\+supplier_name+\+supplier_address+\argeName= \\'\+supplier_chargeName+\

\\'\+supplier_phone+\+supplier_email+\\\'\+supplier_QQ+\\\'\+supplier_remark+\where supplierID = \+ fID;

try{

Class.forName(\);

Connection con =

DriverManager.getConnection(\

15