您的位置:首页 > 数据库

SQL Server简单连接对象[note]

2018-01-06 20:07 155 查看
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Warehouse {
/**
* @author oneslide
* @see this url default is:"jdbc:sqlserver://localhost:1433;DatabaseName=workspace"
* **/
public Warehouse(String url, String username, String password) {
super();
this.url = url;
this.username = username;
this.password = password;
try {
Class.forName(driver);
this.connection=DriverManager.getConnection(this.url,this.username,this.password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private String url;
private String username;
private String password;
private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private Connection connection;

//set and get
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Connection getConnection() {
return connection;
}

}


使用技巧:数据库连上了,最终目的是获得Connection对象。

如果你要连接的数据库没有你这个用户名,新建一个,

选择
db.owner


public class TestDatabase {

public static void main(String[] args) {
// TODO Auto-generated method stub

Warehouse house=new Warehouse("jdbc:sqlserver://localhost:1433;DatabaseName=workspace","oneslideicywater","oiuuuo7uu");
System.out.println(house.getConnection()+"hello world");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: