JDBC 连接数据库

package org.yingmm.entity.conn.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import org.yingmm.entity.conn.entity.FieldInfo;

/**
 * @ClassName: JdbcConnHelp
 * @Date:2015-1-12 下午05:13:23
 * @author yingmm
 * @description: 数据库连接 辅助类
 */
public class JdbcConnHelp {

	private static final String driver = "oracle.jdbc.driver.OracleDriver";
	private static String url = "jdbc:oracle:thin:@192.168.1.190:1521:orclvids";
	private static final String user = "scott";
	/** 注:请设置用户密码 */
	private static final String password = "123456";

	/*
	 * private static final String driver =
	 * TemplateUtil.getTemplateUtil().getProperties("jdbc.driver",""); private static String
	 * url=TemplateUtil.getTemplateUtil().getProperties("jdbc.url",""); private static final String
	 * user = TemplateUtil.getTemplateUtil().getProperties("jdbc.user",""); private static final
	 * String password = TemplateUtil.getTemplateUtil().getProperties("jdbc.password","");
	 */

	private static Connection getConnection = null;

	/**
	 * getConnections:开启session会话
	 * 
	 * @author yingmm
	 * @Date:2015-1-13下午04:27:22
	 * @description
	 */
	public static Connection getConnections() {

		try {
			Class.forName(driver);
			getConnection = DriverManager.getConnection(url, user, password);
			System.out.println("★★★★★★★★★★--jdbc连接数据库成功...用户:" + user + "-★★★★★★★★★★★★★★");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getConnection;
	}

	/**
	 * closeConnection:关闭session会话
	 * 
	 * @author yingmm
	 * @Date:2015-1-13下午04:27:52
	 * @description
	 */
	public static void closeConnection(ResultSet rs, Statement statement, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (statement != null) {
				statement.close();
			}
			if (conn != null) {
				conn.close();
			}
			System.out.println("★★★★★★★★★★--session连接已关闭--★★★★★★★★★★★★★★");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * getSchema:得到用户的方案名称
	 * 
	 * @author yingmm
	 * @Date:2015-1-13下午04:28:11
	 * @description
	 */
	public static String getSchema() throws Exception {
		String schema;
		schema = getConnection.getMetaData().getUserName();
		if ((schema == null) || (schema.length() == 0)) {
			throw new Exception("ORACLE数据库模式不允许为空");
		}
		return schema.toUpperCase().toString();
	}

	/**
	 * main:测试连接
	 * 
	 * @author yingmm
	 * @date:Mar 23, 2015 9:07:08 PM
	 * @description
	 */
	public static void main(String[] args) {
		Connection conn = null;
		Statement statement = null;
		ResultSet rs = null;

		try {
			conn = JdbcConnHelp.getConnections();
			statement = conn.createStatement();

			String query = " select * from scott.dept ";

			rs = statement.executeQuery(query);
			ResultSetMetaData rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();
			for (int i = 1; i <= columnCount; i++) {
				Class<?> classas = Class.forName(rsmd.getColumnClassName(i));
				System.out.println(classas.getSimpleName());
				if (classas.isAssignableFrom(java.math.BigDecimal.class)) {
					System.out.println("Ttttttttt");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcConnHelp.closeConnection(rs, statement, conn);
		}
	}

}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。