java遠端連線SQLServer2005伺服器
阿新 • • 發佈:2019-02-18
import java.sql.*;
public class DatabaseConn {
public void getConnection(){
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String dburl="jdbc:odbc:conn";
String dbuser="sa";
String dbpwd="123";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection(dburl,dbuser,dbpwd);
if(con!=null){
System.out.println("feikong");
}
else {
System.out.println("kong");
}
stmt=con.createStatement();
String query="select * from UserInfo";
rs=stmt.executeQuery(query);
if(rs.next()){
int id=rs.getInt(1);
System.out.println(id);
}
}catch(SQLException e){
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
if(con!=null){
con.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
public class DatabaseConn {
public void getConnection(){
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String dburl="jdbc:odbc:conn";
String dbuser="sa";
String dbpwd="123";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection(dburl,dbuser,dbpwd);
if(con!=null){
System.out.println("feikong");
}
else {
System.out.println("kong");
}
stmt=con.createStatement();
String query="select * from UserInfo";
rs=stmt.executeQuery(query);
if(rs.next()){
int id=rs.getInt(1);
System.out.println(id);
}
}catch(SQLException e){
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
if(con!=null){
con.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}