我的数据库操作工具类

数据库操作工具类,不用重复写了。web中的。

package dbUtils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtils {
	private static String driver;
    private static String url;
    private static String username;
    private static String password;
    
    private Connection conn=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
    //static语句块初始化字段信息
    static{
        try {
            Properties properties = new Properties();
            properties.load(DBUtils.class.getClassLoader().getResourceAsStream("./WEB-INF/db.properties"));
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	public DBUtils() {
		// TODO Auto-generated constructor stub
		try{
			Class.forName(driver);
			System.out.println("数据库连接成功!");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 获得数据库的连接
	 * @return
	 */
	public Connection getConnection(){
		try {
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 查询
	 * @return
	 */
	public ResultSet select(String sql)throws Exception{
		try{
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery(sql);
			return rs;
			
		}catch(SQLException sqle){
			throw new SQLException("select data Exception: "+sqle.getMessage());
		}catch(Exception e){
			throw new Exception("System error: "+e.getMessage());
		}
	}
	
	/**
	 * 插入数据
	 * @return
	 */
	public int insert(String sql) throws Exception{
		int num = 0;
		try{
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			num = ps.executeUpdate();
			
		}catch(SQLException sqle){
			throw new SQLException("insert data Exception: "+sqle.getMessage());
		}finally{
			try{
				if(ps!=null){
					ps.close();
				}
			}catch(Exception e){
				throw new Exception("ps close exception: "+e.getMessage());
			}
			try{
				if(conn!=null){
					conn.close();
				}
			}catch(Exception e){
				throw new Exception("conn close exception: "+e.getMessage());
			}
		}
		return num;
	}
	
	/**
	 * 删除数据
	 * @return
	 */
	public int delete(String sql) throws Exception{
		int num = 0;
		try{
			conn = getConnection();
			ps = conn.prepareStatement(sql);//sql为删除语句
			num = ps.executeUpdate();//返回影响条数
			
		}catch(SQLException sqle){
			throw new SQLException("delete data Exception: "+sqle.getMessage());
		}finally{
			try{
				if(ps!=null){
					ps.close();
				}
			}catch(Exception e){
				throw new Exception("ps close exception: "+e.getMessage());
			}
			try{
				if(conn!=null){
					conn.close();
				}
			}catch(Exception e){
				throw new Exception("conn close exception: "+e.getMessage());
			}
		}
		return num;
	}
	
	/**
	 * 更新数据
	 * @return
	 */
	public int update(String sql) throws Exception{
		int num = 0;
		try{
			conn = getConnection();
			ps = conn.prepareStatement(sql);//sql为删除语句
			num = ps.executeUpdate();//返回影响条数
			
		}catch(SQLException sqle){
			throw new SQLException("update data Exception: "+sqle.getMessage());
		}finally{
			try{
				if(ps!=null){
					ps.close();
				}
			}catch(Exception e){
				throw new Exception("ps close exception: "+e.getMessage());
			}
			try{
				if(conn!=null){
					conn.close();
				}
			}catch(Exception e){
				throw new Exception("conn close exception: "+e.getMessage());
			}
		}
		return num;
	}
	
}
应用:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%@page import="dbUtils.DBUtils" %>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>

<%
DBUtils dbUtils =new DBUtils();
String sql = "select *from dataanalyse.Dairy where DairyID=3338";
ResultSet rs = dbUtils.select(sql);
int i=1;
while(rs.next()){
	System.out.println(rs.getString(1));
	System.out.println(rs.getString(2));
	System.out.println(rs.getString(3));
	System.out.println(rs.getString(4));
}

 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ShowData.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">
	-->

  </head>
  
  <body>
  武开英
  </body>
</html>

java中的:

http://blog.csdn.net/yanzi1225627/article/details/26950615

这篇文章写的很详细。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值