java连接sql2008数据库的问题,我是擦考之前连接mysql改的,不知道为什么连接不上,请指教
packageutil;importjava.sql.Connection;importjava.sql.DriverManager;publicclassDBsqlk{...
package util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBsqlk {
private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库驱动
private static final String url="jdbc:sqlserver://192.168.199.89:1433;DatabaseName=service";//连接数据库的URL地址
private static final String username="sa";//数据库用户名
private static final String password="111111";//数据库用户密码
private static Connection conn=null;
//静态代码块负责加载驱动
static
{
try
{
Class.forName(driver);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
//
public static Connection getConnection() throws Exception
{
if(conn==null)
{
conn = DriverManager.getConnection(url, username, password);
return conn;
}
return conn;
}
public static void main(String[] args) {
try
{
Connection conn = DBmysql.getConnection();
if(conn!=null)
{
System.out.println("数据库连接正常");
}
else
{
System.out.println("数据库连接异常");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
} 展开
import java.sql.Connection;
import java.sql.DriverManager;
public class DBsqlk {
private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库驱动
private static final String url="jdbc:sqlserver://192.168.199.89:1433;DatabaseName=service";//连接数据库的URL地址
private static final String username="sa";//数据库用户名
private static final String password="111111";//数据库用户密码
private static Connection conn=null;
//静态代码块负责加载驱动
static
{
try
{
Class.forName(driver);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
//
public static Connection getConnection() throws Exception
{
if(conn==null)
{
conn = DriverManager.getConnection(url, username, password);
return conn;
}
return conn;
}
public static void main(String[] args) {
try
{
Connection conn = DBmysql.getConnection();
if(conn!=null)
{
System.out.println("数据库连接正常");
}
else
{
System.out.println("数据库连接异常");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询