<%@ 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" >
<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>
<form name= "Form1" method= "Post" action= "002.jsp" >
<p> </p>
<p>name:
<label for= "textfiled" ></label>
<input type= "text" name= "myname" id= "textfiled1" >
</p>
<p>class:
<label for= "textfiled" ></label>
<input type= "text" Name= "MyClass" id= "Textfiled2" >
</p>
<input type= "Submit" name= "button" id= "button" value= "Submit" >
</form>
</body>
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<jsp:usebean id= "myjdbc01" scope= "page" class= "Com.lty.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" >
<base href= "<%=basePath%>" >
<title>my JSP ' 002.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>
<%=myname%><br>
<%=myclass%><br>
<%
String insertsql= "INSERT into message (Name,class) VALUES ('" +myname+ "', '" +myclass+ "')";
Myjdbc01.doinsert (Insertsql);
Myjdbc01.close ();
%>
</body>
<%@ 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.lty.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>
<%
String sql= "SELECT * from Message";
ResultSet rs = myjdbc01.doselect (SQL);
Out.println ("<table width= ' border= ' 1 ' >");
Out.println ("<tr>");
Out.println ("<th scope= ' col ' > Study No. </th>");
Out.println ("<th scope= ' col ' > class </th>");
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 ("</table>");
%>
</body>
Package com.lty.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/student";
String user= "root";
String password= "1234";
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.executeinset:" + 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= ' 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 block
E.printstacktrace ();
}
Rs.close ();
Db.close ();
}
}
JSP Job 2017.4.5