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

[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 List query(Connection conn, String sql,

IResultSet irs, Object... parames) {

List lists = null;

try {

lists = new ArrayList();

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 List query(String sql, IResultSet irs,

Object... parames) {

Connection conn = null;

List 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;

}

}

694748ed64b9390909c0d88230893790.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值