JAVA配置SQL2012

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) {

}
}
}
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。