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

📄 datamanagerimp.java

📁 servlet实现网上书店查询
💻 JAVA
字号:
package com.ghy.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DataManagerImp implements DataManager {

	private String driver;
	private String url;
	private String username;
	private String password;
	private Connection connection;

//	public void initDB() throws IOException {
//		//Thread.currentThread()返回当前执行线程对象的引用
//		//getContextClassLoader()返回该线程的上下文 ClassLoader
//		ClassLoader cl = Thread.currentThread().getContextClassLoader(); 
//		//返回读取指定资源的输入流。
//		InputStream is = cl.getResourceAsStream("ghyjdbc.properties");
//		//Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。
//		//属性列表中每个键及其对应值都是一个字符串。 
//		Properties properties = new Properties();
//		//从输入流中读取属性列表(键和元素对)。
//		properties.load(is);
//		this.driver = properties.getProperty("jdbc.driverClassName");
//		this.url = properties.getProperty("jdbc.url");
//		this.username = properties.getProperty("jdbc.username");
//		this.password = properties.getProperty("jdbc.password");
//		is.close();
//		is = null;
//	}
	public void initDB() throws IOException
	{
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		InputStream is = cl.getResourceAsStream("oraclejdbc.properties");
		Properties properties = new Properties();
		properties.load(is);
		this.driver = properties.getProperty("jdbc.driverClassName");
		this.url = properties.getProperty("jdbc.url");
		this.username = properties.getProperty("jdbc.username");
		this.password = properties.getProperty("jdbc.password");
		is.close();
		is=null;
		
	}

	public void beginTransaction(boolean supportTran) {
		// TODO Auto-generated method stub

	}

	public void closeDBConnection() throws SQLException {
		connection.close();
		connection = null ;

	}

	public void commitTransaction() throws SQLException {
		connection.commit();

	}

	public Connection getConnection() throws IOException,
			InstantiationException, IllegalAccessException,
			ClassNotFoundException, SQLException {
//		initDB();
//		Class.forName(this.driver).newInstance();
//		connection = DriverManager.getConnection(this.url, this.username,
//				this.password);
//		connection.setAutoCommit(true);
//		return connection;
		initDB();
		Class.forName(this.driver).newInstance();
		connection = DriverManager.getConnection(this.url, this.username, this.password);
		connection.setAutoCommit(true);
		return connection;
	}

	public boolean rollbackTransaction() throws SQLException {
		if(connection!=null)
		{
			connection.rollback();
			return true;
			
		}
		return false;

	}

}

⌨️ 快捷键说明

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