Create a new Web project name called MYWEBJDBC, create a new com.zss.www package under the SRC folder, and build a Java file in the package: dbconn;
Create 3 JSP files under Webroot.
Build a school library in MySQL and build a table in it: Information, which includes Name,class.
The Dbconn code is as follows:
Packagecom.zss.www;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importcom.mysql.jdbc.Connection;Importcom.mysql.jdbc.PreparedStatement; Public classDbconn {PrivateConnection conn=NULL; PrivatePreparedStatement stmt=NULL; PrivateResultSet rs=NULL; Publicdbconn () {Try{String driver= "Com.mysql.jdbc.Driver"; String URL= "Jdbc:mysql://localhost:3306/school"; String User= "Root"; String Password= "11"; Class.forName (driver); Conn=(Connection) drivermanager.getconnection (Url,user,password); System.out.println ("-------Connection Successful------"); } Catch(ClassNotFoundException classnotfoundexception) {classnotfoundexception.printstacktrace (); System.err.println ("DB:" +classnotfoundexception.getmessage ()); } Catch(SQLException SQLException) {System.err.println ("Db.getconn ():" +sqlexception.getmessage ()); } } Publicdbconn (String driver,string url,string user,string password) {Try { //driver= "Com.mysql.jdbc.Driver"; //url= "Jdbc:mysql://Localhost:3306/school "; //user= "root"; //password= "11"; Class.forName (driver); Conn=(Connection) drivermanager.getconnection (Url,user,password); System.out.println ("-------Connection Successful------"); } Catch(ClassNotFoundException classnotfoundexception) {classnotfoundexception.printstacktrace (); System.err.println ("DB:" +classnotfoundexception.getmessage ()); } Catch(SQLException SQLException) {System.err.println ("Db.getconn ():" +sqlexception.getmessage ()); } } Public voidDoinsert (String sql) {Try{stmt=(PreparedStatement) conn.preparestatement (SQL); inti =stmt.executeupdate (SQL); } Catch(SQLException SQLException) {System.err.println ("Db.executeinset:" +sqlexception.getmessage ()); }finally{ } } Public voiddoDelete (String sql) {Try{stmt=(PreparedStatement) conn.preparestatement (SQL); inti =stmt.executeupdate (SQL); } Catch(SQLException SQLException) {System.err.println ("Db.executedelete:" +sqlexception.getmessage ()); } } Public voiddoupdate (String sql) {Try{stmt=(PreparedStatement) conn.preparestatement (SQL); inti =stmt.executeupdate (SQL); } Catch(SQLException SQLException) {System.err.println ("Db.executeupdate:" +sqlexception.getmessage ()); } } PublicResultSet doselect (String sql) {Try{stmt=(PreparedStatement) conn.preparestatement (SQL); RS=stmt.executequery (SQL); System.out.println ("Get result set"); } Catch(SQLException SQLException) {System.err.println ("Db.executequery:" +sqlexception.getmessage ()); } returnrs; } Public voidClose (ResultSet rs)throwsSQLException, Exception {if(rs! =NULL) {rs.close (); RS=NULL; } if(stmt! =NULL) {stmt.close (); stmt=NULL; } if(Conn! =NULL) {conn.close (); Conn=NULL; } } Public voidClose ()throwsSQLException, Exception {if(stmt! =NULL) {stmt.close (); stmt=NULL; } if(Conn! =NULL) {conn.close (); Conn=NULL; } } Public Static voidMain (String []args)throwsException {dbconn db=Newdbconn (); ResultSet RS=db.doselect ("select * from Dbuser where username= ' Chenjirong '"); Try { while(Rs.next ()) {System.out.println (rs.getstring (1)); System.out.println (Rs.getstring (2)); System.out.println (Rs.getstring (3)); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } rs.close (); Db.close (); } }
1.JSP:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > for= "TextField" ></label> <input type= "text" name= "myname" id= "TextField" > </p> <p>class= & Lt;label for= "Textfield2" ></label> <input type= "text" Name= "MyClass" id= "Textfield2" > </p> <p> < Input type= "Submit" name= "button" id= "button" value= "Submit" > </p></form> </body>2.JSP:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><jsp:usebean id= "myjdbc01" scope= "page" class= "Com.zss.www.DBConn"/><%request.setcharacterencoding ("Utf-8"); String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; String myname=request.getparameter ("MyName"); String MyClass=request.getparameter ("MyClass");%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >String Insertsql= "INSERT into information (Name,class) VALUES ('" +myname+ "', '" +myclass+ "')"; Myjdbc01.doinsert (Insertsql); Myjdbc01.close (); %> </body>
3.JSP:
<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%@ page import= "java.sql.*"%><%@ Page import= "com.mysql.jdbc.*"%><jsp:usebean id= "myjdbc01" scope= "page" class= "Com.zss.www.DBConn"/><%request.setcharacterencoding ("Utf-8"); String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >String SQL= "SELECT * FROM Information"; ResultSet RS=myjdbc01.doselect (SQL); while(Rs.next ()) {out.println (rs.getstring (1)); Out.println (Rs.getstring (2)); } %> </body>Run as follows:
1.JSP:
MySQL under:
Standalone Operation 3.jsp:
JDBC Operations under the WEB (JSP)