package com.xs.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestDb {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL ="jdbc:sqlserver://localhost:1433;databastName=myxs";
private static final String USER = "sa";//这个代表你的数据库登录名
private static final String PWD = "icss";//这个代表你的数据库登录密码,一般是空的。
private Connection con = null;
public TestDb(){
try {
Class.forName(DRIVER);//加载数据库驱动
con = DriverManager.getConnection(URL,USER,PWD);//连接数据库
System.out.println("数据库连接成功");
} catch (SQLException e) {
System.out.println("驱动加载失败");
// TODO Auto-generated catch blo-QWAwaaAck
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("数据库连接失败");
e.printStackTrace();
}
}
public Connection getConnection(){
return con;
}
public void close(){
try {
this.con.close();
System.out.println("数据库成功关闭");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("数据库关闭失败");
e.printStackTrace();
}
}
}