database.sarang.net
UserID
Passwd
Database
DBMS
ㆍMySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MySQL Tutorials 16582 게시물 읽기
 News | Q&A | Columns | Tutorials | Devel | Files | Links
No. 16582
JSP/Servlet 에서 MySQL 접속 예제코드
작성자
정재익(advance)
작성일
2002-07-22 07:09
조회수
8,102

<%@ page import=\java.sql.*\ contentType=\text/html; charset=euc-kr\%>

 

Mysql test

이 페이지는 Mysql 접속 테스트입니다.


 

<%

Connection conn = null;

Statement stmt = null;

ResultSet rs;

String sql;

 

String db_driver = \org.gjt.mm.mysql.Driver\;

 

String db_server = \localhost\;

String db_name = \test\;

String db_url = \jdbc:mysql://\ + db_server + \/\ + db_name;

String db_uid = \;

String db_pwd = \;

String db_table = \testa\;

 

try {

Class.forName(db_driver);

out.print(\Step 1. JDBC to access : success !
\);

}

catch(ClassNotFoundException e){

out.print(\Step 1. JDBC to access : failed !
\);

out.print(e.getMessage() + \
\);

out.close();

}

 

try {

conn = DriverManager.getConnection(db_url, db_uid, db_pwd);

out.print(\Step 2. mysql to access : success !
\);

}

catch(SQLException e){

out.print(\Step 2. mysql to access : failed !
\);

out.print(e.getMessage() + \
\);

out.close();

}

 

String command=request.getParameter(\command\);

 

if (command != null && command.equals(\insert\)) { // Insert data

try {

stmt = conn.createStatement() ;

String name=request.getParameter(\name\);

if (name != null && !name.equals(\)) {

sql = \insert into \ + db_table + \(name) values(\ + name + \)\;

//sql = \insert into \ + db_table + \(name) values(\ + makeKor_insert(name) + \)\;

stmt.executeQuery(sql);

}

 

stmt.close();

}

catch (SQLException e){

out.print(e.getMessage() + \
\);

conn.close();

out.close();

}

}

 

// View data

try {

stmt = conn.createStatement() ;

sql = \select * from \ + db_table;

 

rs = stmt.executeQuery(sql) ;

 

if (rs.next()) out.print(\

\); // name not exist

rs.beforeFirst(); // to first rs

while (rs.next()) {

out.print(\idx = \ + rs.getString(1) + \ , name = \ + rs.getString(2) + \
\);

//out.print(\idx = \ + rs.getString(1) + \ , name = \ + makeKor_get(rs.getString(2)) + \
\);

}

 

rs.close();

stmt.close();

conn.close();

}

catch (SQLException e){

out.print(e.getMessage() + \
\);

conn.close();

out.close();

}

finally {

// if ( rs != null ) try {rs.close();}catch(Exception e){}

if ( stmt != null ) try {stmt.close();}catch(Exception e){}

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

}

%>

 


\>

이름입력
 

?command=list\>입력된 데이터 보기

 

 

<%!

//get db

public String d_c(String d)

{

if (d == null) return \;

return d;

}

 

public String makeKor_get(String s)throws java.io.UnsupportedEncodingException

{

String kor=\;

if (s==null) kor=null;

else kor=new String(s.getBytes(\ISO-8859-1\),\EUC-KR\);

return kor;

}

 

 

//insert db

public String makeKor_insert(String s)throws java.io.UnsupportedEncodingException

{

String kor=\;

if (s==null) kor=null;

else kor=new String(s.getBytes(\ISO-8859-1\),\EUC-KR\);

kor=new String(kor.getBytes(\KSC5601\),\8859_1\);

return kor;

}

%>

 

[Top]
No.
제목
작성자
작성일
조회
16599PHP/MySQL Tutorial (3)
정재익
2002-07-23
8237
16596PHP/MySQL Tutorial (2)
정재익
2002-07-23
10319
16595PHP/MySQL Tutorial (1)
정재익
2002-07-23
32256
16582JSP/Servlet 에서 MySQL 접속 예제코드
정재익
2002-07-22
8102
16581NT4.0+JDK+JSDK+apache+tomcat+mysql+jdbc
정재익
2002-07-22
8288
16580MySQL 기본사용법
정재익
2002-07-22
22305
16578mSQL 자료를 mySQL 로 변경하기
정재익
2002-07-22
6319
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2023 DSN, All rights reserved.
작업시간: 0.048초, 이곳 서비스는
	PostgreSQL v16.1로 자료를 관리합니다