关于jdbc连接sqlServer的脚下留心!

package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class sqlserverJDBC {

public static void main(String[] args) {
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=bo","sa","123456");
ps = conn.prepareStatement("select * from [dbo].[user]");
rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+""+rs.getString(2));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

}

你可能感兴趣的:(java,sql,jdbc,Microsoft)