JAvabeansカプセル化データベース接続操作


Datasql.java
 package data;
import java.sql.*;

public class Datasql {
    private final String url="127.0.0.1/";
    private final String database="book";
    private final String usename="root";
    private final String password="yy3948389";
    private final String driver="com.mysql.jdbc.Driver";
    private String connstr="jdbc:mysql://";
    private String sqlstr;
    Connection conn=null;
    ResultSet rs=null;
    Statement stmt=null;
    public Datasql(){
        try{
            conn=openconn();
            stmt=conn.createStatement();
        }catch(Exception e){
            System.err.println(" ");
        }
    }
    public Connection openconn()throws Exception,SQLException{
        try{
            Class.forName(driver).newInstance();
        }catch(Exception e){
            System.err.println(" ");
        }
        return conn=DriverManager.getConnection(connstr+url+database,usename,password);
    }
    public ResultSet select(String sqlstr){
        this.sqlstr=sqlstr;
        try{
            rs=stmt.executeQuery(sqlstr);
        }catch(SQLException e){
            System.err.println(sqlstr+": ");
        }
        return rs;
    }
    public boolean updata(String sqlstr){
        this.sqlstr=sqlstr;
        try{
            stmt.executeUpdate(sqlstr);
            return true;
        }catch(SQLException e){
            System.err.println(sqlstr+": ");
            return false;
        }
    }
    public boolean insert(String sqlstr){
        this.sqlstr=sqlstr;
        try{
            stmt.executeUpdate(sqlstr);
            return true;
        }catch(SQLException e){
            System.err.println(sqlstr+": ");
            return false;
        }
    }
    public boolean delete(String sqlstr){
        this.sqlstr=sqlstr;
        try{
            stmt.executeUpdate(sqlstr);
            return true;
        }catch(SQLException e){
            System.err.println(sqlstr+": ");
            return false;
        }
    }
    public boolean close(){
        try{
            if(rs!=null){
                rs.close();
            }
            if(stmt!=null){
                stmt.close();
            }
            if(conn!=null){
                conn.close();
            }
            return true;
        }catch(Exception e){
            System.err.println(" !");
            return false;
        }
    }
}
show.jsp
 
  <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> </title>
</head>
<style type="text/css">
<!--
.STYLE1 {
    font-size: 14px;
    color: #FFFFFF;
    font-weight: bold;
    font-family: " ";
}
body,td,th {
    font-family: ;
    font-size: 12px;
    color: #000000;
}
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
-->

</style>
<body>
<jsp:useBean id="sqlbean" scope="page" class="data.Datasql"/>
<table width="80%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
  <tr>
    <td height="30" colspan="2" align="center" valign="middle" bgcolor="#0099FF"><span class="STYLE1"> <%=new java.text.SimpleDateFormat(" yyyy d mm ").format(new java.util.Date())%></span></td>
  </tr>
<%
ResultSet rs=sqlbean.select("select name from admin");
int i=0;
while(rs.next()){
String name=rs.getString("name");
i++;
%>
  <tr>
    <td width="15%" height="20" align="center" valign="middle"><%=i%></td>
    <td width="85%" align="center" valign="middle"><%=name%></td>
  </tr>
<%
}
if(!sqlbean.close()){
    out.println(" ");
}
%>
</table>
</body>
</html>