WEB(JSP)下的JDBC操作实验

001.jsp下的代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>






My JSP '001.jsp' starting page










name=



class=








 

 

002.jsp下的代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%
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");
%>






My JSP '002.jsp' starting page









<%=myname %>

<%=myclass %>


<%
String insertsql="insert into information(name,class) values('"+myname+"','"+myclass+"')";
myjdbc01.doInsert(insertsql);
myjdbc01.close();
%>

 

 

DBConn的JavaBean代码

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("------连接成功----");
} 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("取得结果集");
} 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();
}

}

 WEB(JSP)下的JDBC操作实验_第1张图片

WEB(JSP)下的JDBC操作实验_第2张图片

WEB(JSP)下的JDBC操作实验_第3张图片

 

你可能感兴趣的:(WEB(JSP)下的JDBC操作实验)