怎么使用JDBC连接SQL Server
2016-08-31 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String connectionUrl = "jdbc:sqlserver://IP:1433;databasename=数据库";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.newInstance();
con = DriverManager.getConnection(connectionUrl, "用户", "密码");
// 调用存储过程
// CallableStatement stat = null;
// stat = con.prepareCall("{call boss(?)}");
// stat.setString(1, "0601");
// rs = stat.executeQuery();
String SQL = "select * from t_userinfo ";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
String codeid = rs.getString("u_name");
String code = rs.getString("u_pwd");
String codedesc = rs.getString("u_code");
System.out.println(codeid + code + codedesc);
}
// 打印表字段
// ResultSetMetaData rsmd = rs.getMetaData();
// int count = rsmd.getColumnCount();
// StringBuilder sBuilder = new StringBuilder();
// for (int i = 1; i <= count; i++) {
// sBuilder.append(rsmd.getColumnName(i)).append(",");
// }
// System.out.println(sBuilder.toString());
} catch (Exception e) {
e.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) {
}
}
}
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String connectionUrl = "jdbc:sqlserver://IP:1433;databasename=数据库";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.newInstance();
con = DriverManager.getConnection(connectionUrl, "用户", "密码");
// 调用存储过程
// CallableStatement stat = null;
// stat = con.prepareCall("{call boss(?)}");
// stat.setString(1, "0601");
// rs = stat.executeQuery();
String SQL = "select * from t_userinfo ";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
String codeid = rs.getString("u_name");
String code = rs.getString("u_pwd");
String codedesc = rs.getString("u_code");
System.out.println(codeid + code + codedesc);
}
// 打印表字段
// ResultSetMetaData rsmd = rs.getMetaData();
// int count = rsmd.getColumnCount();
// StringBuilder sBuilder = new StringBuilder();
// for (int i = 1; i <= count; i++) {
// sBuilder.append(rsmd.getColumnName(i)).append(",");
// }
// System.out.println(sBuilder.toString());
} catch (Exception e) {
e.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) {
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询