数据库连接类-无实例

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

//不能继承
public final class JdbcUtils {

 private static String url = "jdbc:sqlserver://localhost:1433;DataBaseName=HXParserDB";

 private static String username = "sa";

 private static String password = "123";

 private JdbcUtils() {
 }

 static {
  try {
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
     .newInstance();

  } catch (Exception e) {
   throw new ExceptionInInitializerError(e);
  }
 }

 public static Connection getConnection() throws Exception {
  return DriverManager.getConnection(url, username, password);
 }

 public static void free(ResultSet rs, PreparedStatement pstmt, Connection conn) {
   try {
   if (rs != null)
    rs.close();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (pstmt != null) {
     pstmt.close();
    }
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    try {
     if (conn != null) {
      conn.close();
     }
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }
 }

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