⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnection.java

📁 博克后台的开发,有很多使用的方法和例子可以提供给大家学习
💻 JAVA
字号:
package com.webpublish.dbutil;

import java.sql.*;
import java.util.Properties;

import com.webpublish.config.ConfigUtil;
import com.webpublish.config.ComStatic;

public class DBConnection {

	public static synchronized Connection getConnOracle(String DB_server,
			String DB_username, String DB_password) throws SQLException {

		Properties sysProps = new Properties();

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			sysProps.put("user", DB_username);
			sysProps.put("password", DB_password);
		} catch (ClassNotFoundException cnfe) {
			System.out.println("无法连接数据库!");
			//ComStatic.GrainLog.errLog("无法连接数据库!");
		}
		return DriverManager.getConnection(DB_server, sysProps);
	}

	public static synchronized Connection getConnOracle() throws SQLException {

		Properties sysProps = new Properties();
		ConfigUtil getConfig = new ConfigUtil("DB.properties");

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			sysProps.put("user", getConfig.getProperty("OraDB_username"));
			sysProps.put("password", getConfig.getProperty("OraDB_password"));
		} catch (ClassNotFoundException cnfe) {
			System.out.println("无法连接数据库");
			//ComStatic.GrainLog.errLog("无法连接数据库!");
		}
		return DriverManager.getConnection(getConfig
				.getProperty("OraDB_server"), sysProps);
	}

	// 测试数据库联接用

	public static void main(String[] args) {
		try {
			Connection conn = getConnOracle("jdbc:oracle:thin:@192.168.0.101:1521:ora815","db66user","cbznw68j");

			String sqlCode = null;
			ResultSet rs = null;
			Statement stmt = null;

			try {
				sqlCode = "select * from db66_ymwh where nrmc='知识生活' order by xssj desc,lbid";
				// where nrmc='知识生活' order by xssj desc,lbid
				 //sqlCode = "select * from ydbuser.VWEB_KLSX";
				// preparedStatement = conn.prepareStatement(sqlCode);
				// preparedStatement.setString(1, "100");
				stmt = conn.createStatement(
						java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
						java.sql.ResultSet.CONCUR_READ_ONLY);
				rs = stmt.executeQuery(sqlCode);
				if (rs.next()) {
					String testString = rs.getString(1);
					rs.last();
					int intRowCount = rs.getRow();
					 System.out.println("结果:" + intRowCount);

					System.out.println(testString);
				}
				rs.close();
			} catch (SQLException e) {
				System.out.println("-----异常:" + e);
			} finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (SQLException sqle) {
						System.out.println("dbTestException1: "
								+ sqle.getMessage());
					}
				}

				if (stmt != null) {
					try {
						stmt.close();
						stmt = null;
					} catch (SQLException sqle) {
						System.out.println("dbTestException2: "
								+ sqle.getMessage());
					}
				}
				try {
					if (conn != null) {
						conn.close();
						conn = null;
					}
				} catch (Exception e) {
					System.out.println("OradbUtil close conn error!");
				}
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -