connectdatabase.java

来自「java数据库编程。JDBC+SQL+GUI。用java写的一个影碟租赁系统」· Java 代码 · 共 59 行

JAVA
59
字号
//Vedio rental System Developed by Banu

// packages for database connection
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// for oracle specific 

import oracle.jdbc.driver.*;
//import oracle.jdbc.pool.OracleDataSource;


public class connectDatabase //Static class for connecting to a database
{
	
	private static String strDriver = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; //Only can do MS Access at the momment
	
	public static Connection getConnection (String strFileName, String strUserName, String strPassWord) throws Exception
	{
		
		Connection databaseConnection;
		
		try
		{
			
			// following code is generated to connct oracle database @ deakin.
			// Since I am getting a connection error as, "The Network Adaptor
			// could not establish the connection."  I am useing MSAcess database.
			// Although I keep this orecle connection code because it may work inside the deakin netwok.

/*
			OracleDataSource ods = new OracleDataSource();
	        ods.setURL("jdbc:oracle:thin:@bajor.its.deakin.edu.au:1527:SSID");

	        // Retrieve a connection and set to databaseConnection.
	        Connection databaseConnection = ods.getConnection("bko","kokulnat");

*/	        
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Asumming it tests if the driver is supported
			strDriver += strFileName.trim() + ";DriverID=22;READONLY=true}"; //Adds to the driver the database address and extra stuff
            databaseConnection = DriverManager.getConnection(strDriver, strUserName, strPassWord); //Create the connection
            
        }
        catch (ClassNotFoundException ex) //Thrown if driver is not reconised
        {	       
	        throw new Exception("\nError loading database access driver.\nPossible reason is that you do not have MS Access");	        
        }
        catch (SQLException ex) //Thrown if database can not be found
        {	        
	        throw new Exception("\nError connecting to database\n" + System.getProperty("user.dir") + "\\" + strFileName + "\nPlease make sure the database exists");	        
        }
        
        return databaseConnection;
        
    }
	
	
}

⌨️ 快捷键说明

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