JDBC combined with JSP (1)

Source: Internet
Author: User
Tags dname

1. Add Data

Adding data to a JSP page is similar to adding data in serv. After obtaining the data submitted in the page, save the data to the database table, the JSP code is as follows:

add.jsp

<%@ page language= "Java"Import= "java.sql.*" pageencoding= "gb2312"%><%request.setcharacterencoding ("GB2312"); String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Connection Conn=NULL; PreparedStatement PS=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); System.out.println ("Creating a database driver is successful!" "); Conn= Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/bank", "root", "1234"); System.out.println ("The database connection was successful!" "); String SQL= "INSERT INTO dept (id,d_name,address,empnumber) VALUES (?,?,?,?)"; PS=conn.preparestatement (SQL); String ID= Request.getparameter ("id"); String D_name= Request.getparameter ("D_name"); String Address= Request.getparameter ("Address"); intEmpnumber = Integer.parseint (Request.getparameter ("Empnumber")); Ps.setstring (1, id); Ps.setstring (2, D_name); Ps.setstring (3, address); Ps.setint (4, Empnumber); intresult =ps.executeupdate (); if(Result = = 1) Out.print ("Insert data Successfully!" "); ElseOut.print ("Insert data failed, please reinsert!" "); }Catch(Exception e) {out.println ("Unable to connect to the database, please check the database connection is correct!" "); }%>
2. Show All data

To display all the data in the page, that is, to query all the data in the database, to implement this function, you need to use the entity class, that is, the database and the Entity object mapping class. The entity class code is as follows:

Deptvo.java

 PackageCom.cn.vo; Public classDeptvo {PrivateString ID; PrivateString address; Private intEmpnumber; PrivateString D_name; Private intd_id;  PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString getaddress () {returnaddress; }     Public voidsetaddress (String address) { This. Address =address; }     Public intGetempnumber () {returnEmpnumber; }     Public voidSetempnumber (intEmpnumber) {         This. Empnumber =Empnumber; }     PublicString Getd_name () {returnD_name; }     Public voidsetd_name (String dname) {d_name=dname; }     Public intgetd_id () {returnd_id; }     Public voidSETD_ID (intDid ) {d_id=Did ; }}

After writing the entity type, you can call the class in the page, write JDBC in the JSP page, connect the database and query the data, and then use the C tag in the JSTL tag library to traverse the output data and use the EL expression to take the value. The code in the JSP is as follows:

showall.jsp

<%@ page language= "Java"Import= "java.util.*" pageencoding= "gb2312"%><%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>< %@ pageImport= "com.cn.vo.*"%><%@ pageImport= "Java.sql.*"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >List<DeptVo> list =NewArraylist<deptvo>(); Try{class.forname ("Com.mysql.jdbc.Driver"); Connection Con= Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/bank", "root", "1234"); Statement stmt=con.createstatement (); ResultSet RS= Stmt.executequery ("SELECT * FROM dept");  while(Rs.next ()) {Deptvo Deptvo=NewDeptvo (); Deptvo.setid (Rs.getstring ("id")); Deptvo.setaddress (Rs.getstring ("Address")); DEPTVO.SETD_ID (Rs.getint ("D_ID")); Deptvo.setd_name (Rs.getstring ("D_name")); Deptvo.setempnumber (Rs.getint ("Empnumber"));          List.add (DEPTVO); } request.setattribute ("List", list);//Put the list collection into the Request object}Catch(Exception e) {e.printstacktrace (); }   %> <body> <table border= "1" align= "center" width= "70%" > <tr> <td> Department number <            /TD> <td> Department Address </td> <td> Department number </td> <td> department name </td>             <td> Department id</td> </tr> <c:foreach items= "${list}" var= "list" > <tr> <td>${list.id}</td> <td>${list.address}</td> <td>${list.        Empnumber}</td> <td>${list.d_name}</td> <td>${list.d_id}</td> </tr> </c:forEach> </table> </body>3. Display a single data message

The display of a single piece of information is based on the unique identifier of the data to query the details of the individual data. In the Dept table, the value of the primary key d_id is automatically increased, there is no repetition, and a single data message can be queried according to D_ID. First, enter the d_id value you want to query on the page, and then query the details of the data based on d_id. Enter the JSP file code for the query criteria as follows:

item.jsp

<%@ page language= "Java"Import= "java.util.*" pageencoding= "gb2312"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

The code for the showbyid.jsp file that appears on the same page is as follows:

<%@ page language= "Java"Import= "java.util.*" pageencoding= "gb2312"%><%@ pageImport= "com.cn.vo.*"%><%@ pageImport= "Java.sql.*"%><% @pageImport= "Javax.servlet.jsp.tagext.TryCatchFinally"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >intd_id = Integer.parseint (Request.getparameter ("d_id")); Connection Con=NULL; PreparedStatement pstmt=NULL; ResultSet RS=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); System.out.println ("Create a drive success!" "); Con= Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/bank", "root", "1234"); System.out.println ("The database connection was successful!" "); String SQL= "SELECT * FROM dept where d_id =?"; Pstmt=con.preparestatement (SQL); Pstmt.setint (1, d_id); RS=Pstmt.executequery ();  while(Rs.next ()) {Deptvo Deptvo=NewDeptvo (); Deptvo.setid (Rs.getstring ("id")); Deptvo.setaddress (Rs.getstring ("Address")); DEPTVO.SETD_ID (Rs.getint ("D_ID")); Deptvo.setd_name (Rs.getstring ("D_name")); Deptvo.setempnumber (Rs.getint ("Empnumber")); Request.setattribute ("Deptvo", Deptvo);              System.out.println (deptvo.getd_id ()); }          }Catch(Exception e) {e.printstacktrace (); }   %> <body> <jsp:include flush= "true" page= "item.jsp" ></jsp:include> 

JDBC combined with JSP (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.