?? dao.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 + -