JSP connection MySQL

Source: Internet
Author: User

Download the Mysql-connector-java-5.1.26-bin.jar driver.

Copy and paste into the directory. Web-inf/lib under

Database:

--Deleting a databaseDROP DATABASE IF EXISTSmldn;--Create a databaseCREATE DATABASEMldnCHARACTER SETUTF8;--Working with Databases Usemldn;--Delete a data tableDROP TABLE IF EXISTSEMP;--Create a data tableCREATE TABLEEMP (empnoINT(4)PRIMARY KEY, enameVARCHAR(Ten), JobVARCHAR(9), HireDate DATE, SalFLOAT(7,2)) ;--inserting test DataINSERT  intoEMP (empno,ename,job,hiredate,sal)VALUES(6060,'Li','Manager','2001-09-16',2000.30) ;INSERT  intoEMP (empno,ename,job,hiredate,sal)VALUES(7369,'nan','Sales','2003-10-09',1500.90) ;INSERT  intoEMP (empno,ename,job,hiredate,sal)VALUES(8964,'Qi','Analyst','2003-10-01', the) ;INSERT  intoEMP (empno,ename,job,hiredate,sal)VALUES(7698,'Hui','Sales','2005-03-12', -) ;INSERT  intoEMP (empno,ename,job,hiredate,sal)VALUES(7782,'Army','Analyst','2005-01-12',2500) ;INSERT  intoEMP (empno,ename,job,hiredate,sal)VALUES(7762,'Ming','Sales','2005-03-09', +) ;INSERT  intoEMP (empno,ename,job,hiredate,sal)VALUES(7839,'Month','Manager','2006-09-01',2500) ;

JSP page

<%@ Page ContentType="text/HTML "pageencoding="GBK"%><%@ Page Import="java.sql.*"%><! DOCTYPE html Public"-//W3c//DTD XHTML1.0Transitional//EN "" http://www.w3.org/Tr/Xhtml1/Dtd/Xhtml1-Transitional.dtd "><HTML xmlns="http://www.w3.org/1999/xHTML><Head><Title>Www.mldnjava.cn,MLDN High-end Java training</Title></Head><Body><%!  PublicStatic final String Dbdriver="Com.mysql.jdbc.Driver";  PublicStatic final String Dburl="Jdbc:mysql://localhost3306/MLDN ";  PublicStatic final String DBUSER="Root";  PublicStatic final String Dbpass="lcp8090";%><%Connection Conn= NULL ; PreparedStatement pstmt= NULL ; ResultSet RS= NULL ;%><%try{Class.forName (dbdriver); Conn=drivermanager.getconnection (Dburl,dbuser,dbpass); String SQL="SELECTEmpno,ename,job,sal,hiredate fromEMP "; Pstmt=conn.preparestatement (SQL); RS=pstmt.executequery ();%><Center><TableBorder="1The width=" the%">    <Tr>        <Td>Employee number</Td>        <Td>Employee Name</Td>        <Td>Employee work</Td>        <Td>Employee wages</Td>        <Td>Date of employment</Td>    </Tr><%     while(Rs.Next()){        intEmpno=Rs.getint (1) ; String ename=Rs.getstring (2) ; String Job=Rs.getstring (3) ; floatSal=Rs.getfloat (4) ; Java.util.Date Date=Rs.getDate(5) ;%>    <Tr>        <Td><%=Empno%></Td>        <Td><%=Ename%></Td>        <Td><%=Job%></Td>        <Td><%=Sal%></Td>        <Td><%=Date%></Td>    </Tr>    <%    }%></Table></Center><%}catch (Exception e) {System.out.println (e);} Finally{Rs.Close() ; Pstmt.Close() ; Conn.Close() ;//If you close the connection directly, you can also}%></Body></Html>

JSP connection MySQL

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.