Util.java
import java.sql.*;
public class Util {
private Connection conn;
private Statement stm;
private ResultSet rs;
private String classname="com.mysql.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306/test";
public Util(){
try {
Class.forName(classname);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/*获取连接*/
public Connection getConn() {
try {
conn=DriverManager.getConnection(url,"root","123456");
} catch (SQLException e) {
e.printStackTrace(System.err);
conn=null;
}
return conn;
}
/*关闭连接*/
public void closed(){
try {
if (rs!=null) {
rs.close();
}
if (stm!=null) {
stm.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void setConn(Connection conn) {
this.conn = conn;
}
/*执行语句*/
public Statement getStm() {
try {
conn=getConn();
stm=conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stm;
}
public Statement getStmed(){
try {
conn=getConn();
stm=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
} catch (SQLException e) {
e.printStackTrace();
}
return stm;
}
public ResultSet getAllRs(){
String sql="select * from u";
try {
stm=getStmed();
rs=stm.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public ResultSet getPartRs(String subsql,String subsqlvalue){
if(subsql==null){
subsql="";
}
if(subsqlvalue==null){
subsqlvalue="";
}
String sql= "select * from u where "+subsql+" ='"+subsqlvalue+"'";
try {
stm=getStmed();
rs=stm.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/*插入数据*/
public int insert(String sql){
int num=-1;
if(sql==null) sql="";
try {
stm=getStmed();
num=stm.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
num=-1;
}
return num;
}
}
index.jsp
<form action="result.jsp" method="post">
<table>
<tr bgcolor="lightgrey" height="25">
<td align="center">Id</td>
<td align="center">用户名</td>
<td align="center">密码</td>
</tr>
<jsp:useBean id="db" class="com.count.Util" scope="page"></jsp:useBean>
<%
ResultSet rsall = db.getAllRs();
while(rsall.next()){
%>
<tr>
<td align="center"><%=rsall.getString("id") %></td>
<td align="center"><%=rsall.getString("name") %></td>
<td align="center"><%=rsall.getString("pwd") %></td>
</tr>
<%
}
%>
<tr>
<td align="center">用户名:<input type="text" name="name"/></td>
<td align="center">密码:<input type="text" name="pwd"/></td>
</tr>
<tr>
<td align="center"><input type="submit" value="添加"/></td>
</tr>
</table>
</form>
result.jsp
<jsp:useBean id="db" class="com.count.Util" scope="page"></jsp:useBean>
<%
String mess="";
String name= request.getParameter("name");
String pwd=request.getParameter("pwd");
%>
<%
name=new String(name.getBytes("utf-8"));
pwd=new String(pwd.getBytes("utf-8"));
String sql="insert into u(name,pwd) values('"+name+"','"+pwd+"')";
int i=db.insert(sql);
db.closed();
if(i>0){
mess="插入成功";
}else{
mess="插入失败";
}
%>
<table>
<tr><td align="center"><%=mess %></td>
<td align="left"><a href="index.jsp">【返回主页】</a></td>
</tr>
</table>
5.PNG
6.PNG
网友评论