package student;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Test {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
//加载驱动
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.println("驱动名称错误!");
}
//建立连接
try{
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:3306;DatabaseName=mysql2015","root","mysql580230");
System.out.println("建立连接成功!");
}catch(SQLException e){
System.out.println("数据库连接字符串,数据库用户名,密码错误!");
e.printStackTrace();
}finally{
try{
if(conn!=null){
conn.close();
System.out.println("关闭连接成功!");
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Test {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
//加载驱动
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.println("驱动名称错误!");
}
//建立连接
try{
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:3306;DatabaseName=mysql2015","root","mysql580230");
System.out.println("建立连接成功!");
}catch(SQLException e){
System.out.println("数据库连接字符串,数据库用户名,密码错误!");
e.printStackTrace();
}finally{
try{
if(conn!=null){
conn.close();
System.out.println("关闭连接成功!");
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}