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

📄 dbconnection.java

📁 着重用css实现页面显示功能,实现简单数据库连接,是很好的入门教程
💻 JAVA
字号:
/*
 * Created on 2006-1-19
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cn.com.aheadsoft.conndb;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.io.Serializable;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DBConnection implements Serializable{
     private String db_url;
     private String db_driver;
     private String db_user;
     private String db_password;
     private Connection con = null;
/**
 * Get a connection
 * @return Connection
 * @throws SQLException this method
 */
    public Connection getConnection() throws SQLException {
    	//System.out.println(db_url);
        con=DriverManager.getConnection(db_url,db_user,db_password);
        return con;
    }

    public DBConnection() {
        init();
    }
    private void init() {
        Configuration cfg = Configuration.getInstance();
        this.db_driver = cfg.getValue("DB.DRIVER");
        this.db_url = cfg.getValue("DB.URL");
        this.db_user = cfg.getValue("DB.USER");
        this.db_password = cfg.getValue("DB.PASSWORD");
        System.out.print(db_driver);
        //this.db_driver ="sun.jdbc.odbc.JdbcOdbcDriver";
       // this.db_url="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=D:\\jzw.mdb";
        //this.db_user="root";
       // this.db_password="aheadsoft";
        try {
                Class.forName(db_driver);
        }catch (ClassNotFoundException ex) {
                System.out.println(ex);
        }
   }
   
    public void CloseCon()throws Exception{
    	
    	if(con != null)
    		 con.close();
    	
    }
    public static void main(String[] args)throws SQLException{
    	DBConnection aa= new DBConnection();
    	if(aa.getConnection()!=null)
    		System.out.println("connect success");
    	else 
    		System.out.println("connect fail");
    	
    }
}


⌨️ 快捷键说明

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