|
|
|
|
|
|
|
|
분류 : 웹호스팅 제목: [JSP] mysql 접속 관련 |
|
글쓴이:김승진 날짜: 2006.02.13. 10:37:11 조회:25898 추천:0 글쓴이IP: |
|
|
파일:
첨부파일이 없습니다. |
|
|
|
|
1) java 파일의 경우 접속 확인 방법
$ cat MysqlTest.java
import java.sql.*;
public class MysqlTest {
public static void main(String args[]){
String DB_URL = "jdbc:mysql://localhost:3306/db_name";
String DB_ID = "db_id";
String DB_PW = "db_password";
Connection con;
try{
Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("1 단계. JDBC Driver 로드 : success");
}catch(ClassNotFoundException e){
System.out.println("1 단계. JDBC Driver 로드 : failure");
System.out.println(e.toString());
}
try{
con = DriverManager.getConnection(URL, DB_ID, DB_PW);
System.out.println("2 단계. MYSQL 접속 : success");
}catch(SQLException e){
System.out.println("2 단계. MYSQL 접속 : failure");
System.out.println(e.getMessage());
} finally {
if(conn !=null) try { conn.close(); } catch(Exception ex){}
}
}
}
하지만 애석하게도 java 명령어를 지원하지 않는 관계로 직접 확인이 불가능합니다. 따라서 2번의 방법으로 확인하시기 바랍니다.
2) jsp파일의 경우 접속하는 방법
$ cat MysqlTest.jsp
<%@ page import="java.sql.*" contentType="text/html;charset=euc-kr"%>
<html>
<head>
<title>mysql 접속 테스트</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<body>
<%
String DB_URL = "jdbc:mysql://localhost:3306/db_name";
String DB_ID = "db_id";
String DB_PW = "db_password";
Connection conn = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
out.println("1 단계. JDBC Driver 로드 : success<br>");
} catch(Exception ex) {
out.println("1 단계. JDBC Driver 로드 : failure<br>");
}
try {
conn = DriverManager.getConnection(DB_URL, DB_ID, DB_PW);
if(conn != null) out.println("2 단계. MYSQL 접속 : success");
} catch(Exception ex) {
out.println("2 단계. MYSQL 접속 : failure");
} finally {
if(conn != null) try { conn.close(); } catch(Exception ex) {}
}
%>
</body>
</html>
위 두 경우 모두 finally 부분을 보시면 어떠한 경우라도 conn 객체는 닫아야 한다는 점에 주의하시기 바랍니다. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |