关闭

SQL Server 数据库链接封

73人阅读 评论(0) 收藏 举报
分类:
package util;

/**
 *创建连接数据库的相关服务,方便调用 
 *@author  wu
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class jdbcutil {
	private static final String DRIVER_CLASS_NAME;
	private static final String URL;
	private static final String USER_NAME;
	private static final String PASSWORD;

	public static Connection getSqlServerConn() {
		return null;

	}

	// 设定数据库相关参数
	static {
		DRIVER_CLASS_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		URL = "jdbc:sqlserver://115.156.***.*:1433;databaseName=SpringJdbcTest";
		USER_NAME = "sa";
		PASSWORD = "123456";
	}

	public static Connection getconnection() {
		Connection conn = null;
		try {
			Class.forName(DRIVER_CLASS_NAME);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	// 测试数据库链接
	public static void main(String[] args) throws SQLException {
		Statement stmt = null;
		ResultSet rs = null;
		String SQL = "SELECT TOP 5 * FROM bookInfo";
		jdbcutil jd = new jdbcutil();
		Connection conn = jd.getconnection();
		try {
			stmt = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(SQL);

		try {
			rs = stmt.executeQuery(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(rs);
		
		stmt.close();
		conn.close();
//		网络测试
		
		  ResultSetMetaData rsmd = rs.getMetaData();
		    int columnsNumber = rsmd.getColumnCount();
		    while (rs.next()) {
		        for (int i = 1; i <= columnsNumber; i++) {
		            if (i > 1) System.out.print(",  ");
		            String columnValue = rs.getString(i);
		            System.out.print(columnValue + " " + rsmd.getColumnName(i));
//		            System.out.println("列:"+columnsNumber);
		        }
		        System.out.println("");
		    }
		
	}

	// 执行sql语句
	public static boolean exeSql(String sql) {
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcutil.getconnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			closeConnection(stmt, conn);
		}
		return true;
	}

	/**
	 * 关闭Statement
	 * 
	 * @param stmt
	 */
	private static void closeStatement(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
	}

	/**
	 * 关闭Statement、Connection
	 * 
	 * @param stmt
	 * @param con
	 */
	private static void closeConnection(Statement stmt, Connection con) {
		closeStatement(stmt);
		closeConnection(con);
	}

	/**
	 * 关闭Connection
	 * 
	 * @param con
	 */
	private static void closeConnection(Connection con) {
		if (con != null) {
			try {
				con.close();
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}

	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:664次
    • 积分:57
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档