JDBC Operation experiment under WEB (JSP)

Source: Internet
Author: User
Tags getmessage stmt

The code under 001.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"
<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>name=
<label for= "TextField" ></label>
<input type= "text" name= "myname" id= "TextField" >
</p>
<p>class=
<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>

The code under 002.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" >
<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 information (Name,class) VALUES ('" +myname+ "', '" +myclass+ "')";
Myjdbc01.doinsert (Insertsql);
Myjdbc01.close ();
%>
</body>

Dbconn's JavaBean code

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;
Import com.mysql.jdbc.Connection;

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 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 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= ' LBX '");
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) {
E.printstacktrace ();
}
Rs.close ();
Db.close ();
}

}

JDBC Operation experiment under WEB (JSP)

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.