public class Sqlcon{
public Connection getConnection(){
Connection conn=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//注册驱动
}catch(Exception e){
e.printStackTrace();
//System.out.println("JDBC驱动不可用");
}
try{
String url="jdbc:sqlserver://localhost:1433;DataBaseName=Student";
String user="sa"
String password=""
conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatemet();
}catch(Exception e){
e.printStackTrace();
//System.out.println("数据库连接失败");
}
return conn;
}
pulic static void main(String[] args){
Sql sql=new Sql();
sql.getConnection();
}
}
网友评论