怎么使用JDBC连接SQL Server
2个回答
展开全部
首先先导入MSSQL的JDBC驱动 然后代码实现如下: package com.mwq.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class JDBC { private static final String DRIVERCLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static final String URL = "jdbc:sqlserver://baiweiming:1433;DatabaseName=db_DrinkeryManage1"; private static final String USERNAME = "sa"; private static final String PASSWORD = ""; private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(); static {// 通过静态方法加载数据库驱动 try { Class.forName(DRIVERCLASS).newInstance();// 加载数据库驱动 } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() {// 创建数据库连接的方法 Connection conn = threadLocal.get();// 从线程中获得数据库连接 if (conn == null) {// 没有可用的数据库连接 try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);// 创建新的数据库连接 threadLocal.set(conn);// 将数据库连接保存到线程中 } catch (SQLException e) { e.printStackTrace(); } } return conn; } public static boolean closeConnection() {// 关闭数据库连接的方法 boolean isClosed = true; Connection conn = threadLocal.get();// 从线程中获得数据库连接 threadLocal.set(null);// 清空线程中的数据库连接 if (conn != null) {// 数据库连接可用 try { conn.close();// 关闭数据库连接 } catch (SQLException e) { isClosed = false; e.printStackTrace(); } } return isClosed; } }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-10-26 · 知道合伙人互联网行家
关注
展开全部
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) {
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询