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

📄 connectiondbbean.java

📁 一个可以连接Access MySQL Oracle SQL Server ..数据库的bean
💻 JAVA
字号:
/*windows 自带了Access和SQL Server的驱动,所以其它数据库需要下载驱动*/
/*这类似于配置文件,最好写在xml文件中*/
import java.sql.*;
import java.io.*;


public class ConnectionDBBean implements Serializable{
	public final static int MSACCESSDB = 0;
	public final static int SQLSERVERDB = 1;
	public final static int MYSQLDB = 2;
	public final static int ORACLEDB = 3;
	private int dbTypes = MSACCESSDB;
	public ConnectionDBBean(int dbTypes){
		this.dbTypes = dbTypes;
	}

	public Connection connectionDB() throws java.lang.Exception{
		
		String driver = "";
		//String url = "jdbc:odbc:DBName";//使用此方法需要配ODBC数据源
		String url = "";
		if(dbTypes == 0){//Access
			driver ="sun.jdbc.odbc.JdbcOdbcDriver";
			url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DB\\UserInfo.mdb";//DBQ后可以是绝对路径也可以是相对路径
		}
		else if(dbTypes == 1){//SQL Server
			driver ="sun.jdbc.odbc.JdbcOdbcDriver";
			url = "jdbc:odbc:DBName";
		}
		else if(dbTypes == 2){//MySQL
			driver ="com.mysql.jdbc.Driver";
			url = "jdbc:mysql://[$host]:3306/[$dbName]";//jdbc:mysql://172.29.2.60:3306/dbName
		}
		else if(dbTypes == 3){//Oracle
			driver ="oracle.jdbc.driver.OracleDriver";
			url = "jdbc:oracle:thin:@[$host]:1521:[$dbName]";//jdbc:oracle:thin:@172.29.2.60:1521:dbName
		}

		String userName  = "";
		String userPassword = "";
		Connection connection = null;
		
			Class.forName(driver);
			connection = DriverManager.getConnection(url,userName,userPassword);
				return connection;

	
	}

	public void closeDB() throws java.lang.Exception{
				connectionDB().close();		
	}

	public void setDBTypes(int dbTypes){
		this.dbTypes = dbTypes;
	}
	public int getDBTypes(){
		return dbTypes;
	}






}


⌨️ 快捷键说明

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