当前位置 : 主页 > 编程语言 > c++ >

基于Oracle数据库的jdbc小框架

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt public static int update(String sql,Object params[]){Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = getConnection();ps = conn.prepareStatement(sql);for(int i=0;i list = new ArrayList();try{c
gistfile1.txt
public static int update(String sql,Object params[]){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			for(int i=0;i
 
   list = new ArrayList();
		try{
			conn = getConnection();
			st = conn.prepareStatement(sql);
			rs = st.executeQuery();
			while(rs.next()){
				User user = new User();
				user.setName(rs.getString("name"));
				user.setPwd(rs.getString("pwd"));
				user.setAge(BigDecimal.valueOf(rs.getInt("age")));
				user.setEmail(rs.getString("email"));
				list.add(user);
			}
			return list;
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			relese(conn, st, rs);
		}	
	}
 
JdbcUtils.java
package cn.ln.Utils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cn.ln.UserBean.User;

public class JdbcUtils {
	private static DataSource ds;
	private static Connection conn = null;
	
	static{
		try {
			Context initCtx = new InitialContext();
			ds = (DataSource) initCtx.lookup("java:comp/env/aaa");
		} catch (NamingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static Connection getConnection() throws SQLException, ClassNotFoundException{		
		conn = ds.getConnection();
		return conn;
}
	
	public static void relese(Connection conn,PreparedStatement ps,ResultSet rs){
		if(rs!=null){
			try{
				rs.close();   //throw new 
			}catch (Exception e) {
				e.printStackTrace();
			}
			rs = null;
		}
		if(ps!=null){
			try{
				ps.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			ps = null;
		}
		if(conn!=null){
			try{
				conn.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static int update(String sql,Object params[]){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			for(int i=0;i
 
   list = new ArrayList();
		try{
			conn = getConnection();
			st = conn.prepareStatement(sql);
			rs = st.executeQuery();
			while(rs.next()){
				User user = new User();
				user.setName(rs.getString("name"));
				user.setPwd(rs.getString("pwd"));
				user.setAge(BigDecimal.valueOf(rs.getInt("age")));
				user.setEmail(rs.getString("email"));
				list.add(user);
			}
			return list;
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			relese(conn, st, rs);
		}	
	}	
}
 
网友评论