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

📄 dao.java

📁 struts_宾馆管理系统
💻 JAVA
字号:
package link;

import java.sql.*;

public class DAO {
	
	//声明连接数据库变量
	private String driver;	//声明驱动
	private String url;		//声明连接字串
	private String name;	//声明用户名
	private String password;	//声明密码
	private String database;	//声明连接数据库
	
	public static Connection conn;
	
	public String getDriver() {
		return driver;
	}
	public void setDriver(String driver) {
		this.driver = driver;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getDatabase() {
		return database;
	}
	public void setDatabase(String database) {
		this.database = database;
	}
	public Connection getConn() {
		return conn;
	}
	public static void setConn(Connection conn) {
		DAO.conn = conn;
	}
	
	//创建构造函数
	public DAO(){
		setDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
		setUrl("jdbc:microsoft:sqlserver://localhost:1433;");
		this.database = "jiudian";
		this.password = "sa";
		this.name = "sa";
	}
	public DAO( String database, String name, String password ){
		setDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
		setUrl("jdbc:microsoft:sqlserver://localhost:1433;");
		this.database = database;
		this.password = password;
		this.name = name;
	}
	
	//连接数据库
	public void connection() throws Exception{
		
		try{
			//连接驱动
			Class.forName( this.getDriver() );
			
			//连接服务器
			conn = DriverManager.getConnection( this.getUrl()+ "DatebaseName=" + this.getDatabase() ,this.getName(),this.getPassword() );
		}

		catch( Exception e ){
			System.out.print( "连接服务器失败" );
			
			throw new Exception("连接服务器失败");
		}
	}
	
	//断开连接
	public boolean close(){
		try{
			this.driver = null;
			this.url = null;
			this.database = null;
			this.name = null;
			this.password = null;
			conn.close();          
			return true;
		}
		catch( Exception e ){
			System.out.print( "关闭服务器失败" );
			return false;
		}
	}

}

⌨️ 快捷键说明

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