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

📄 accessdbconnection.java

📁 一个可以存储和打印成绩单的系统
💻 JAVA
字号:
package org.wuhang.transfer;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class AccessDBConnection {
    Connection con = null;
    String db_path = null;
	private String type = null;
	public AccessDBConnection(String type){
		this.type = type;
	}
	
    private boolean setJDbName(){
		DBCopy copy = new DBCopy();
		db_path = copy.CopyDB();
		if(db_path == null)
			return false;
		else{
			return true;
	    }
	}
	
    private boolean setADbName(){
    	JFileChooser chooser = new JFileChooser();
    	chooser.setDialogTitle("选择数据库");
    	int returnVal = chooser.showOpenDialog(null);
		if(returnVal == JFileChooser.APPROVE_OPTION) {
			if (isDB(chooser.getSelectedFile().getName())) {
				db_path = chooser.getSelectedFile().getAbsolutePath();
				db_path = db_path.replace('\\', '/');
				return true;
			}else
				return false;
		}
		else
			return false;
    }
    
    private boolean isDB(String path){
    	String t_path = path.substring(path.lastIndexOf(".")+1, path.length());
    	if("adb".equalsIgnoreCase(t_path)||"mdb".equalsIgnoreCase(t_path)){
    		return true;
    	}
    	else {
    		JOptionPane.showMessageDialog(null, "或所选取的数据库不合法!", "提示", JOptionPane.PLAIN_MESSAGE);
			return false;
		}
    }
    
    public Connection getConnection() {
    	String url1 = null;
    	if ("javadb".equals(type)) {
			if (setJDbName()) {
				url1 = "jdbc:odbc:driver={microsoft access driver (*.mdb)};dbq="
						+ db_path + ";pwd=12-343";
			} else
				return null;
		} else if ("access".equals(type)) {
			if(setADbName()){
				url1 = "jdbc:odbc:driver={microsoft access driver (*.mdb)};dbq="
					+ db_path + ";pwd=12-343";
			}else
				return null;
        }
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con = DriverManager.getConnection(url1);
			return con;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} 
  
	}
}

⌨️ 快捷键说明

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