Code snippet under 003.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"
<base href= " <%=basePath%>
<title>my JSP ' 001.jsp ' starting page</title>
<meta http-equiv= "Pragma" content= "No-cache",
<meta http-equiv= "Cache-control" content= "No-cache";
< Meta http-equiv= "expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3"
<meta http-equiv= "description" content= "This is my page";
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css";
-->
<body>
<%
Out.println ("<table width= ' border= ' 1 ' >");
Out.println ("<tr>");
Out.println ("<th scope= ' col ' > Study No. </th>");
Out.println ("<th scope= ' col ' > class </th>");
Out.println ("</tr>");
String sql= "SELECT * from Information";
ResultSet rs = myjdbc01.doselect (SQL);
while (Rs.next ()) {
Out.println ("<tr>");
Out.println ("<td>");
Out.println (rs.getstring (1));
Out.println ("</td>");
Out.println ("<td>");
Out.println (rs.getstring (2));
Out.println ("</td>");
Out.println ("</tr>");
}
Out.println ("</table>");
%>
</body>
Code under the Dbconn.java class
Package com.zss.www;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import com.mysql.jdbc.Connection;
Import com.mysql.jdbc.PreparedStatement;
public class Dbconn {
Private Connection Conn=null;
Private PreparedStatement Stmt=null;
Private ResultSet Rs=null;
Public Dbconn () {
try {
String driver= "Com.mysql.jdbc.Driver";
String url= "Jdbc:mysql://localhost:3306/school";
String user= "root";
String password= "lbx110";
Class.forName (driver);
conn = (Connection) drivermanager.getconnection (Url,user,password);
SYSTEM.OUT.PRINTLN ("-------Connection successfully------");
} catch (ClassNotFoundException classnotfoundexception) {
Classnotfoundexception.printstacktrace ();
System.err.println ("DB:" + classnotfoundexception.getmessage ());
} catch (SQLException SQLException) {
System.err.println ("Db.getconn ():" + sqlexception.getmessage ());
}
}
Public Dbconn (String driver,string url,string user,string password) {
try {
Driver= "Com.mysql.jdbc.Driver";
Url= "Jdbc:mysql://localhost:3306/school";
User= "Root";
Password= "711109";
Class.forName (driver);
conn = (Connection) drivermanager.getconnection (Url,user,password);
SYSTEM.OUT.PRINTLN ("-------Connection successfully------");
} catch (ClassNotFoundException classnotfoundexception) {
Classnotfoundexception.printstacktrace ();
System.err.println ("DB:" + classnotfoundexception.getmessage ());
} catch (SQLException SQLException) {
System.err.println ("Db.getconn ():" + sqlexception.getmessage ());
}
}
public void Doinsert (String sql) {
try {
stmt = (preparedstatement) conn.preparestatement (SQL);
int i = stmt.executeupdate (sql);
} catch (SQLException SQLException) {
System.err.println ("Db.executeinsert:" + sqlexception.getmessage ());
}finally{
}
}
public void DoDelete (String sql) {
try {
stmt = (preparedstatement) conn.preparestatement (SQL);
int i = stmt.executeupdate (sql);
} catch (SQLException SQLException) {
System.err.println ("Db.executedelete:" + sqlexception.getmessage ());
}
}
public void DoUpdate (String sql) {
try {
stmt = (preparedstatement) conn.preparestatement (SQL);
int i = stmt.executeupdate (sql);
} catch (SQLException SQLException) {
System.err.println ("db.executeupdate:" + sqlexception.getmessage ());
}
}
Public ResultSet 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 ());
}
Return RS;
}
public void Close (ResultSet rs) throws SQLException, Exception {
if (rs! = null) {
Rs.close ();
rs = null;
}
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
IF (conn! = null) {
Conn.close ();
conn = null;
}
}
public void Close () throws SQLException, Exception {
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
IF (conn! = null) {
Conn.close ();
conn = null;
}
}
public static void Main (String []args) throws Exception {
Dbconn db=new dbconn ();
ResultSet rs=db.doselect ("select * from Dbuser where username= ' Lin '");
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 block
E.printstacktrace ();
}
Rs.close ();
Db.close ();
}
}
Application of JDBC