Thuc Hanh Java

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Thuc Hanh Java as PDF for free.

More details

  • Words: 603
  • Pages: 7
-1Tạo thư mục WEB-INF chứa các thư mục : classes,source,lib,tabs và tập tin XML web.xml. Trong thư mục source chứa các file java,javabean. Trong thư mục classes chứa các class. Tập tin XML có nội dung sau : ----------web.xml-----------<web-app> <welcome-file-list> <welcome-file>login.jsp Các trang JSP nằm cùng cấp với thư mục WEB-INF: -------------login.jsp-------------------<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> Untitled Document <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <% if(request.getParameter("btlogin")==null) { %>


-2
Login
Username
Password

new account

<% } else { %> <jsp:useBean id="bean" class="myclass.QLSach" scope="application"> <jsp:setProperty name="bean" property="strDriver" value="sun.jdbc.odbc.JdbcOdbcDriver"/> <jsp:setProperty name="bean" property="strUrl" value="jdbc:odbc:QLSach"/> <% String user=request.getParameter("textuser"); String pass=request.getParameter("textpass"); boolean kt=false; try { kt=bean.checkuser(user,pass); } catch(Exception ex) { out.println("

Loi roi

"); } if(kt==true) response.sendRedirect("book.jsp"); else out.println("

Invalid user

"); } %> ------------newacc.jsp----------------<%@ page language="java" import="myclass.QLSach" %> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> Create Account

-3<% if(request.getParameter("btnewaccount")==null) { %>

Create Account

Username
Password

<% } else { %> <jsp:useBean id="bean" class="myclass.QLSach" scope="application"/> <% String username=request.getParameter("textuser"); String password=request.getParameter("textpass"); try { bean.insertAccount(username,password); out.println("

Create account succesfull

"); } catch(Exception ex) { out.println("

Create account false

"); } } %>

-4-

------------book.jsp--------------<%@ page language="java" import="myclass.*" import="java.sql.*" %> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> New Page 1

List Books

<jsp:useBean id="bean" class="myclass.QLSach" scope="application"/> <% try { ResultSet rs=null; try { rs=bean.getbook(); } catch(Exception ex) { ex.printStackTrace(); } while(rs.next()) { String strtam=""; strtam+=""; strtam+=""; strtam+=""; strtam+=""; strtam+=""; out.println(strtam); } } catch(NullPointerException exn)

-5{ exn.printStackTrace(); } %>
BookCode BookName BookPrice
"+rs.getInt("BookCode")+""+rs.getString("BookName")+""+rs.getInt("BookPrice")+"

NewBook

--------------newbook.jsp-------------<%@ page language="java" import="myclass.QLSach" %> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> New Page 1 <% if(request.getParameter("btnewbook")==null) { %>

New Book

BookCode
BookName
BookPrice

<% } else { %> <jsp:useBean id="bean" class="myclass.QLSach" scope="application"/>

-6<% String bookCode=request.getParameter("textcode"); String bookName=request.getParameter("textname"); String bookPrice=request.getParameter("textprice"); try { bean.insertBook(bookCode,bookName,bookPrice); out.println ("

Insert Sussesfull

"); } catch(Exception ex) { out.println ("

Insert Fails

"); } } %> ----------------QLSach.java----------------------package myclass; import java.sql.*; public class QLSach { private String strDriver,strUrl; public QLSach() { } public void setStrDriver(String driver) { strDriver=driver; } public String getStrDriver() { return strDriver; } public void setStrUrl(String url) { strUrl=url; } public String getStrUrl() { return strUrl; } public boolean checkuser(String username,String pass) throws Exception { Class.forName(strDriver); Connection con=DriverManager.getConnection(strUrl);

-7Statement sttm=con.createStatement(); String cautruyvan="select * from Account where Username='"+username+"' and Password='"+pass+"'"; ResultSet rs=sttm.executeQuery(cautruyvan); boolean kt; if(rs.next()) { kt=true; } else { kt=false; } return kt; } public void insertAccount(String user,String pass) throws Exception { Class.forName(strDriver); Connection con=DriverManager.getConnection(strUrl); String cautruyvan="INSERT INTO Account VALUES (?,?)"; PreparedStatement pre=con.prepareStatement(cautruyvan); pre.setString(1,user); pre.setString(2,pass); pre.addBatch(); pre.executeBatch(); } public ResultSet getbook() throws Exception { Class.forName(strDriver); Connection con=DriverManager.getConnection(strUrl); Statement sttm=con.createStatement(); ResultSet rs=sttm.executeQuery("Select * from Books"); return rs; } public void insertBook(String code,String name,String price) throws Exception { int bookCode=Integer.parseInt(code); int bookPrice=Integer.parseInt(price); Class.forName(strDriver); Connection con=DriverManager.getConnection(strUrl); String cautruyvan="INSERT INTO Books VALUES (?,?,?)"; PreparedStatement pre=con.prepareStatement(cautruyvan); pre.setInt(1,bookCode); pre.setString(2,name); pre.setInt(3,bookPrice); pre.addBatch(); pre.executeBatch(); } }

Related Documents

Thuc Hanh Java
November 2019 24
Thuc Hanh Excel
November 2019 24
Bai Thuc Hanh 1
November 2019 26
Thuc Hanh Vba
November 2019 18
Danh Sach Thuc Hanh
October 2019 19
Phan Thuc Hanh
June 2020 11