SQL Server 数据库链接封

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());
			}
		}

	}
}

阅读更多
上一篇Struts 2文件的上传与下载
想对作者说点什么? 我来说一句

sqljdbc.jar

2011年08月12日 968KB 下载

java操作SQLServer数据库jar包

2018年03月31日 437KB 下载

没有更多推荐了,返回首页

关闭
关闭