标签:
1、下载JDBC驱动,安装;
2、右击src文件构建配置路径添加sqljdbc4.jar添加外部Jar文件
3、再测试
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;
public class test_sql {
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// System.out.println("加载驱动成功");
con = DriverManager
.getConnection(
"jdbc:sqlserver://192.168.1.2:1433;DatabaseName=dscfp2p",
"sa", "ABcd123");
// System.out.println("连接成功");
String sql = "SELECT * FROM T_BankInfo";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(2) + "\t" + rs.getString(3)
+ "\t" + rs.getString(4) + "\t" + rs.getString(5));
}
} catch (Exception ex) {
// System.out.println("连接失败");
ex.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/weijiafd/p/4543505.html