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

📄 db.java

📁 这是一个图书管理系统源代码
💻 JAVA
字号:
package datebase;

import java.sql.*;

import exception.*;

public class DB {

	private static DB db; //指向自己的对象
	private Connection con;
	private boolean    conFree=true;
	
	private String jdbc;
	private String dbUrl;
	private String datebase;
	private String user;
	private String password;
	
	
	//私有构造函数,防止在外部实例化它,构造函数
	private DB() throws Exception {
		super();
		// TODO Auto-generated constructor stub
		//Connection con=null;
		try {
			/*jdbc="com.microsoft.jdbc.sqlserver.SQLServerDriver";
			dbUrl="jdbc:microsoft:sqlserver://localhost:1433";
			user="sa";
			password="sa";
			datebase="library";*/
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
			System.out.println("connecting ....");
			String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=liabary"; 
			String user1="sa"; 
			String password1="sa"; 
			con= DriverManager.getConnection(url,user1,password1); 
			
			//Class.forName(jdbc).newInstance();
			//con = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;Database=liabary;user=sa;password=sa");
			//st = con.createStatement();
			//results = stmt.executeQuery(sql);
			conFree=true;
			} 
		catch (SQLException e) {
			 throw new ConnectDateBaseException();
		} catch (IllegalAccessException e2) {
			throw new ConnectDateBaseException();
		} catch (ClassNotFoundException e3) {
			throw new ConnectDateBaseException();
		} catch (InstantiationException e4) {
			throw new ConnectDateBaseException();
		}
		
	}
	
	//得到DB的实例,加锁,只有一个实例
	public static synchronized DB getinstance()throws Exception{
		if(db==null)
			db = new DB();
		return db;
	}
	
	//获取一个连接
    protected synchronized Connection getConnection(){
        while (conFree == false) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }

        conFree = false;
        notify();

        return con;
    }
    
    //释放一个连接,这样这个连接就可以被其他地方所用
    protected synchronized void releaseConnection() {
        while (conFree == true) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }

        conFree = true;
        notify();
    }
    

    //关闭连接
    public void remove() {
        try {
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
  
    protected void insert(String sql) throws Exception{
    	try{
    		con=this.getConnection();
    		Statement st=con.createStatement();
    		st.executeUpdate(sql);
    		releaseConnection();
    	}
    	catch(SQLException e){
    		releaseConnection();
    		throw new Exception(e.getMessage());
    	}  	
    }

    protected void update(String sql) throws Exception{
    	insert(sql);
    }
    protected void delet(String sql) throws Exception{
    	insert(sql);
    }
    public ResultSet select(String sql) throws Exception{
    	ResultSet rs=null;
      	try{
      		con=this.getConnection();
    		Statement st=con.createStatement();
    		rs=st.executeQuery(sql);
    		releaseConnection();
    	}
    	catch(SQLException e){
    		releaseConnection();
    		throw new Exception(e.getMessage());
    	}  	
    	return rs;
    }
}

⌨️ 快捷键说明

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