java中连接SQL Server 数据库实例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestJDBC {

public static void main(String[] args) {
getConnection();
}

static final String DRIVER = “com.microsoft.sqlserver.jdbc.SQLServerDriver”;
static final String URL = “jdbc:sqlserver://localhost:1433;databaseName=bank”;
static final String USER = “sa”;
static final String PWD = “123456”;
public static Connection getConnection()
{
Connection con = null;
try{
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USER,PWD);
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(con!=null)
{
System.out.println(“sql server 数据库连接成功!”);
return con;
}
else{
System.out.println(“sql server 数据库连接失败!”);
return null;
}
}
}

标签