'connection'에 해당되는 글 1건

  1. 2008.10.04 Java JDBC 이용한 MySql connection (1)
posted by 희정냥★ 2008. 10. 4. 10:09
  
public class DBConnection {

 

 final static String DB_CONNECTION_URL = "jdbc:mysql://localhost:3306/";

 final static String DB_NAME = "testdb";

 final static String DB_USER_ID = "root";

 final static String DB_USER_PASSWORD = "";

 

 static {

  try {

   Class.forName("com.mysql.jdbc.Driver");

  } catch (ClassNotFoundException e) {

  }

 }
 public static Connection openConnection() {

  Connection con = null;

  try {

   String url = DB_CONNECTION_URL;

   String db = DB_NAME;

   String option = "?useUnicode=true&characterENCODING=euckr";

   url = url + db+ option;

   String id = DB_USER_ID;

   String passwd = DB_USER_PASSWORD;

   con = DriverManager.getConnection(url, id, passwd);

   

   

  } catch (SQLException ex) {

    // handle any errors

      System.out.println("SQLException: " + ex.getMessage());

      System.out.println("SQLState: " + ex.getSQLState());

      System.out.println("VendorError: " + ex.getErrorCode());

  }
  return con;

 }
 public static void closeConnection(Connection con) {

  if (con != null) {

   try {

    con.close();

   } catch (SQLException e) {

   }

  }

 }

 

}



쓰기 위해선 라이브러리에 jar 파일도 추가시켜줘야 합니다~

http://dev.mysql.com/downloads/connector/j/5.0.html 여기서 다운 받으세요.

'Computer > DB' 카테고리의 다른 글

Postgresql 9.1 Replication  (0) 2012.07.07
Java JDBC 이용한 MySql connection  (1) 2008.10.04

댓글을 달아 주세요

  1. BlogIcon 박명학 2009.05.07 10:49  Addr  Edit/Del  Reply

    정리를 깔끔하게 했네요 ^^