thêm, xóa, cập nhật dữ liệu phần 1 - Pdf 62

COMPUTER LEARNING CENTER WWW.HUUKHANG.COM
Môn học: Java Server Pages

BÀI 8: THÊM, XOÁ, CẬP NHẬT DỮ LIỆU

Sau khi chúng ta đã làm quen với JDBC (Java Database
Connectivity), bạn có thể sử dụng cầu nối JDBC-ODBC hay các gói kết
nối khác để xoá, cập nhật, thêm và truy vấn dữ liệu bất kỳ.
Những vấn đề chính sẽ được đề cập trong bài học này
9 Thêm dữ liệu
9 Xoá dữ liệu
9 Cập nhật dữ liệu
9 Thực thi thủ tục nội tại của SQL Server

1.
THÊM DỮ LIỆU
Để thêm mẩu tin vào bảng dữ liệu SQL Server, bạn sử dụng phương thức executeUpdate như ví
dụ trang insert.jsp sau:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ include file="common.jsp"%>
<html>
<head>
<title>Thêm mẩu tin vào cơ sở dữ liệu trong JSP</title>
<LINK href="style.css" rel=stylesheet>
<LINK href="newstyle.css" rel=stylesheet>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html ; charset=utf-8">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%

</body>
</html>

2.
CẬP NHẬT DỮ LIỆU
Trong trường hợp cập nhật dữ liệu cũng tương tự như thêm mẩu tin, bạn sử dụng phương thức
executeUpdate để thực thi phát biểu SQL dạng Update. Chẳng hạn, chúng ta tham khảo ví dụ về
cập nhật dữ liệu như trang update.jsp sau:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ include file="common.jsp"%>
<html>
<head>
<title>Cập nhật mẩu tin vào cơ sở dữ liệu trong JSP</title>
<LINK href="style.css" rel=stylesheet>
<LINK href="newstyle.css" rel=stylesheet>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html ; charset=utf-8">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%
boolean isok=false;
int j=0;
try{
Connection cn;
Statement smt;
ResultSet rst;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection(odbc,sysuser,syspwd);
smt = cn.createStatement();


Phạm Hữu Khang [email protected]
COMPUTER LEARNING CENTER WWW.HUUKHANG.COM
<%@ include file="common.jsp"%>
<html>
<head>
<title>Xoá mẩu tin vào cơ sở dữ liệu trong JSP</title>
<LINK href="style.css" rel=stylesheet>
<LINK href="newstyle.css" rel=stylesheet>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html ; charset=utf-8">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%
boolean isok=false;
int j=0;
try{
Connection cn;
Statement smt;
ResultSet rst;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection(odbc,sysuser,syspwd);
smt = cn.createStatement();
String strSQL="";
strSQL="delete from tblCategories where CateID>=30 ";
j= smt.executeUpdate(strSQL);
isok=true;
smt.close();
cn.close();
}
catch (Exception e)

AddCategories ‘Phan Tich‘ Phạm Hữu Khang [email protected]
COMPUTER LEARNING CENTER WWW.HUUKHANG.COM
Sau đó, từ trang JSP bạn khai báo để thực thi thủ tục này như sau:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ include file="common.jsp"%>
<html>
<head>
<title>Thêm mẩu tin vào cơ sở dữ liệu trong JSP</title>
<LINK href="style.css" rel=stylesheet>
<LINK href="newstyle.css" rel=stylesheet>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html ; charset=utf-8">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%
boolean isok=false;
int j=0;
try{
Connection cn;
Statement smt;
ResultSet rst;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection(odbc,sysuser,syspwd);
smt = cn.createStatement();
String myName ="Phan Tich";
String strSQL="";

select @@identity as No Lưu ý rằng, khi gọi thủ tục này trong SQL Server bạn sử dụng cú pháp như sau:

Phạm Hữu Khang [email protected]
COMPUTER LEARNING CENTER WWW.HUUKHANG.COM

getCategoryID ‘Phan Tich He Thong‘

Thì kết quả trả về là số tự động tương ứng với mã Category. Sau đó, từ trang JSP bạn khai báo
để thực thi thủ tục này bằng đối tượng ResultSet như trang procedurewithvalue.jsp sau:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ include file="common.jsp"%>
<html>
<head>
<title>Thêm mẩu tin vào cơ sở dữ liệu trong JSP</title>
<LINK href="style.css" rel=stylesheet>
<LINK href="newstyle.css" rel=stylesheet>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html ; charset=utf-8">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%
boolean isok=false;
int j=0;
try{
Connection cn;
Statement smt;

Bạn có thể thực thi một thủ tục nội tại có giá trò trả về là một tập dữ liệu của SQL Server
cũng như một phát biểu SQL dạng Select, chẳng hạn chúng ta có thủ tục thực thi những hành
động nào đó, sau đó liệt kê mẩu tin của bảng tblCategories như trang procedurewithresultset.jsp
sau:

create proc getResultset
@id int

Phạm Hữu Khang [email protected]


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status