用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

使用jtds连接数据库,采用泛型

2014-05-28 作者: 陌上之猪举报

[java]代码库

package com.dtl.rmgr.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class DB {
	private static String url;
	private static String username;
	private static String password;
	private static String classDriver;

	static {
		try {
			InputStream in = DB.class.getResourceAsStream("/jdbc.properties");
			Properties prop = new Properties();
			prop.load(in);
			in.close();
			url = prop.getProperty("url");
			username = prop.getProperty("username");
			password = prop.getProperty("password");
			classDriver = prop.getProperty("classDriver");
			Class.forName(classDriver);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// static {
	// try {
	// url = "jdbc:oracle:thin:@localhost:1521:oratlq";
	// username = "rmgr";
	// password = "rmgr";
	// classDriver = "oracle.jdbc.OracleDriver";
	// Class.forName(classDriver);
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	// }

	// 获取一个连接
	public static Connection getConn() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

	// 关闭一个连接
	public static void closeConn(Connection conn) {
		if (null != conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// 执行增,删,改
	public static int update(Connection conn, String sql, Object... parames) {
		int count = 0;
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < parames.length; i++) {
				pstmt.setObject(i + 1, parames[i]);
			}
			count = pstmt.executeUpdate();
			pstmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	public static int update(String sql, Object... parames) {
		Connection conn = null;
		try {
			conn = getConn();
			return update(conn, sql, parames);
		} finally {
			closeConn(conn);
		}
	}

	// 执行查询 找到则放回lists 否则为空
	public static <T> List<T> query(Connection conn, String sql,
			IResultSet<T> irs, Object... parames) {
		List<T> lists = null;
		try {
			lists = new ArrayList<T>();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < parames.length; i++) {
				pstmt.setObject(i + 1, parames[i]);
			}
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				// 读取一行放到lists 集合中
				T t = irs.readRow(rs);
				lists.add(t);
			}
			rs.close();
			pstmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lists;
	}

	public static <T> List<T> query(String sql, IResultSet<T> irs,
			Object... parames) {
		Connection conn = null;
		List<T> lists = null;
		try {
			conn = getConn();
			lists = query(conn, sql, irs, parames);
		} finally {
			DB.closeConn(conn);
		}
		return lists;
	}
	
	
	public static int getMaxId(String sql) {// 获取数据库中department表的最大id
		int id = 1;
		Connection conn = DB.getConn();
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				id += rs.getInt(1);
			}
			rs.close();
			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return id;
	}

}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...