DBManager

package com.struts.util.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

public class DBManager {

 private static final String DRIVER = "com.mysql.jdbc.Driver";
 //private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

 private static final String URL = "jdbc:mysql://localhost:3306/test";
 //private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";

 private static final String USERNAME = "root";

 private static final String PASSWORD = "root";

 static {

  try {
   Class.forName(DRIVER);
  } catch (ClassNotFoundException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
 }

 public static Connection getConnection() {

  Connection conn = null;

  try {
   conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  } catch (SQLException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }

  return conn;
 }

 public static void closeAll(ResultSet rs, Statement state, Connection conn) {

  try {
   if (rs != null)
    rs.close();

   if (state != null)
    state.close();

   if (conn != null)
    conn.close();
  } catch (SQLException e) {

  }

 }

 public static void main(String[] args) {
  Connection conn = DBManager.getConnection();
 }
}

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