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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值