<%@ 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){}
}
%>
<%!
//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;
}
%>
|