JDBC µå¶óÀ̹ö·Î Æ÷½ºÆ®±×·¹½º¸¦ ÀÀ¿ëÇÑ ÇÁ·Î±×·¥À» Å×½ºÆ®Çϱâ Àü¿¡ Æ÷½ºÆ®±×·¹½º µð¸ó ÀÌ È°¼ºÈ µÇ¾î ÀÖ¾î¾ß ÇÏ¸ç µ¥¸óÀº ´ÙÀ½ÀÇ ¿É¼Ç(i)¿¡ ÀÇÇØ ±âµ¿µÇ¾îÁ®¾ß ÇÑ´Ù.
postmaster -i &
ÀÌ°ÍÀº ³×Æ®¿öÅ©¸¦ ÅëÇØ ¿¬°áÇÏ·Á´Â JDBC¸¦ À§ÇØ Çʼö »çÇ×ÀÌ´Ù. ¶ÇÇÑ pg_hba.conf ÆÄÀÏÀ» È®ÀÎÇϵµ·Ï ÇÑ´Ù. ¸¸¾à, ÀϹÝÀûÀÎ IPÁÖ¼Ò°¡ ¾Æ´Ñ localhost(127.0.0.1)À» »ç¿ëÇÏ·ÁÇÑ´Ù¸é ÀÌ ÁÖ ¼Ò(127.0.0.1)¿¡ ´ëÇÏ¿© pg_hba.conf ¿¡ ¼³Á¤ÇÏ¿©¾ß ÇÑ´Ù. ´ÙÀ½Àº localhost ¼³Á¤ ¿¹ÀÌ´Ù.
host all 127.0.0.1 255.255.255.255 trust
JDBC µå¶óÀ̹öÀÇ ·Îµù ¹æ¹ýÀº Class.forName() ¸¦ »ç¿ëÇÏ¸é µÈ´Ù. ÁÖÀÇÇÒ Á¡ÀÌ Àִµ¥ Æ÷ ½ºÆ®±×·¹½º 6.5.x ¹öÀü°ú 7.0.x ¹öÀü´ë¿¡¼ ¾à°£ÀÇ Â÷ÀÌ°¡ »ý°å´Ù. ÇÊÀÚ°¡ Å×½ºÆ®¸¦ ¸ñÀûÀ¸ ·Î ÀÌÀü¿¡ ¸¸µé¾î ³õ¾Ò´ø ÇÁ·Î±×·¥À¸·Î 7.0.x ¹öÀü´ëÀÇ JDBC µå¶óÀ̹ö¸¦ Å×½ºÆ®¸¦ Çغ» °á °ú ¿¡·¯°¡ ¹ß»ýÇÏ¿´´Ù. ¿¡·¯¸¦ ÇØ°áÇϱâ À§ÇØ 7.0.x ¹öÀüÀÇ ¹®¼¸¦ ã¾Æº» °á°ú ¾Æ¹«·± º¯µ¿ ÀÌ ¾ø¾úÁö¸¸ example µð·ºÅ丮ÀÇ ¿¹Á¦ ¼Ò½º¸¦ º¸°í ¿øÀÎÀ» ¹ß°ßÇÏ¿´´Ù.
Æ÷½ºÆ®±×·¹½º 6.5.x ¹öÀüÀÇ JDBC µå¶óÀ̹ö´Â Class.forName("postgresql.jar"); ÇÏ¸é µÇÁö¸¸ 7.0.x ¹öÀüÀÇ JDBC µå¶óÀ̹ö´Â Class.forName("org.postgresql.jar");¸¦ ÇÏ¿©¾ß ÇÑ´Ù. ÀÌ·¯ÇÑ Â÷À̸¦ ÁÖÀÇÇϱ⠹ٶõ´Ù. ´ÙÀ½Àº Æ÷½ºÆ®±×·¹½º ¹öÀü´ëº°·Î Class.forName()¸¦ »ç¿ëÇÑ ¿¹ÀÌ ´Ù. ±âŸ ¿¹¿Ü 󸮸¦ À§ÇØ ClassNotFoundExceptionÀ» »ç¿ëÇÑ´Ù.
public void test()
throws ClassNotFoundException, FileNotFoundException,
IOException, SQLException
{
String url = "jdbc:postgresql:nogadax";
String usr = "postgres";
String pwd = "";
Class.forName("postgresql.Driver");
Connection db = DriverManager.getConnection(url, usr, pwd);
........
........
}
public void test()
throws ClassNotFoundException, FileNotFoundException,
IOException, SQLException
{
String url = "jdbc:postgresql:nogadax";
String usr = "postgres";
String pwd = "";
Class.forName("org.postgresql.Driver");
Connection db = DriverManager.getConnection(url, usr, pwd);
........
........
}
Æ÷½ºÆ®±×·¹½ºÀÇ µ¥ÀÌÅͺ£À̽º·ÎÀÇ ¿¬°áÀ» À§ÇÑ ¿¹´Â ´ÙÀ½°ú °°´Ù.
jdbc:postgresql:database_name
jdbc:postgresql://host/database_name
jdbc:postgresql://host:port/database_name
Æ÷½ºÆ®±×·¹½º DB ¼¹öÀÇ È£½ºÆ® ³×ÀÓÀÌ´Ù. IP¸¦ Á־ µÈ´Ù. ¸¸¾à ÀÌ ºÎºÐÀÌ »ý·«µÇ¸é µðÆúÆ®´Â localhost(127.0.0.1) ÀÌ´Ù.
Æ÷½ºÆ®±×·¹½ºÀÇ µðÆúÆ® Æ÷Æ®´Â 5432 ÀÌ¸ç »ý·« °¡´ÉÇÏ´Ù.
µ¥ÀÌŸº£À̽º¸í
ÄÁ³ØÆ®Çϱâ À§ÇØ JDBC ·ÎºÎÅÍ Connection ÀνºÅϽº¸¦ ¾ò¾î¾ß ÇÑ´Ù. À̸¦ À§ÇØ DriverManager.getConnection() ¸Þ¼Òµå¸¦ »ç¿ëÇØ¾ß ÇÑ´Ù.
Connection db = DriverManager.getConnection(url,user,pwd);
±âŸ Æ÷½ºÆ®±×·¹½º´Â ÇϳªÀÇ ·¹ÄÚµåÀÇ Å©±â°¡ 8K ¹ÙÀÌÆ®·Î Á¦ÇѵǾî ÀÖ´Ù. ¸¸¾à ÇϳªÀÇ ·¹ Äڵ尡 8K ¹ÙÀÌÆ®¸¦ ³Ñ¾î¾ß ÇÑ´Ù¸é "Large Object" ¶ó´Â ¹æ¹ýÀ» »ç¿ëÇÏ¿©¾ß ÇÑ´Ù. ÀÌ¿¡ ´ë Çؼ´Â Æ÷½ºÆ®±×·¹½º ÇÁ·Î±×·¡¸Ó °¡À̵åÀÇ JDBC ºÎºÐÀ» ÂüÁ¶Çϱ⠹ٶõ´Ù. ¶ÇÇÑ, JDBC ¸¦ À§ÇÑ ³ª¸ÓÁö ÀÚ¼¼ÇÑ ºÎºÐÀº ÀÏ¹Ý JDBC ¹®¼µéÀ» ÂüÁ¶Çϱ⠹ٶõ´Ù.
´ÙÀ½Àº Apache-Jserv ¿Í postgreSQLÀÌ ¿¬µ¿µÇ¾úÀ» ¶§ Å×½ºÆ®¸¦ À§ÇÑ ¼Ò½ºÀÌ´Ù. ´ÙÀ½ÀÇ ¼Ò½º´Â Æ÷½ºÆ®±×·¹½ºÀÇ ¿¹Á¦ ¼Ò½º·Î¼ ´ÜÁö servlet ·Î ¹Ù²Ù¾î web »ó¿¡¼ ½ÇÇàÀÌ µÇ°Ô²û ÇØÁØ °ÍÀÌ´Ù.
´ÙÀ½ ¿¹Á¦¸¦ ÄÄÆÄÀÏÇÏ°í ½ÇÇàÇϱ⿡ ¾Õ¼ Æ÷½ºÆ®±×·¹½º¿¡ µ¥ÀÌŸº£À̽º¸¦ ¸¸µé¾î¾ß ÇÑ´Ù. ¿©±â¼´Â µ¥ÀÌŸº£À̽º À̸§ÀÌ nogadax ÀÌ´Ù. À̸¦ ÀûÀýÈ÷ ¹Ù²Ù±â ¹Ù¶õ´Ù. ¶ÇÇÑ Æ÷½ºÆ®±×·¹ ½ºÀÇ À¯Àú¸íÀº postgres ÀÌ°í Æнº¿öµå´Â ¾ø´Ù. ÀÌ°÷ÀÇ ¼Ò½º¿¡¼ÀÇ url, usr, pwd ÀÇ ¼³Á¤Àº ´ÙÀ½°ú °°´Ù. À̸¦ ÂüÁ¶ÇÏ¿© ÀÚ½ÅÀÇ È¯°æ¿¡ ¸Â°Ô ¹Ù²Ù±â ¹Ù¶õ´Ù.
String url = "jdbc:postgresql:nogadax";
String usr = "postgres";
String pwd = "";
ÇÊÀÚÀÇ Å×½ºÆ® ȯ°æÀº ÇÑ´ëÀÇ PC ¿¡ Æ÷½ºÆ®±×·¹½º¿Í ¾ÆÆÄÄ¡°¡ °°ÀÌ ¼³Ä¡µÇ¾î ÀÖ´Ù. ¸¸¾à Å×½ºÆ®ÇÏ·Á°í Çϴ ȯ°æÀÌ DB¼¹ö¿Í WEB¼¹ö°¡ ¼·Î ´Ù¸¥ °÷¿¡ ¼³Ä¡µÇ¾î ÀÖ´Ù¸é, Áï, 3-Tier ȯ°æÀ̸é URL Àº À§¿Í´Â ´Ù¸£°Ô µÇ¾îÁ®¾ß ÇÑ´Ù.
ÀÚ½ÅÀÇ È¯°æÀ» Àû¿ëÇϱâ À§ÇØ ´ÙÀ½ÀÇ ¿¹¸¦ ÂüÁ¶Çϱ⠹ٶõ´Ù. Âü°í·Î IP ÁÖ¼Ò 210.110.144.162 ´Â Æ÷½ºÆ®±×·¹½º DB ¼¹öÀÇ IPÁÖ¼ÒÀ̸ç Æ÷Æ® ¹øÈ£°¡ »ý·«µÇ¾úÀ¸¹Ç·Î µðÆúÆ® ¹øÈ£ "5432"·Î ÀÚµ¿ ¼ÂÆÃµÈ »óÅÂÀÌ´Ù.
String url= "jdbc:postgresql://210.110.144.162/nogadax";
±âŸ Class.forName()À» ¹öÀü¿¡ µû¶ó ÀûÀýÈ÷ ¼öÁ¤Çϱ⠹ٶõ´Ù. ¾Æ·¡ÀÇ Å×½ºÆ®¿ëÀÇ ¼Ò½º´Â Æ÷½ºÆ®±×·¹½º 6.5.3 ÀÇ JDBC µå¶óÀ̹ö¸¦ ±âÁØÀ¸·Î ÀÛ¼ºµÇ¾úÀ¸¹Ç·Î Class.forName()ÀÌ ¾Æ·¡ ¿Í °°´Ù.
Class.forName("postgresql.Driver");
±×·¯¹Ç·Î 7.0.x ÀÇ JDBC µå¶óÀ̹ö¿¡¼ Å×½ºÆ®¸¦ ÇÏ·Á¸é "org.postgresql.Driver" ·Î ¼öÁ¤ÇÏ ¸é µÈ´Ù.
¶ÇÇÑ ÄÄÆÄÀÏµÈ ¼ºí¸´Àº ¸®Æ÷ÁöÅ丮¿¡ ÀÖ¾î¾ß ÇÑ´Ù. ¸®Æ÷ÁöÅ͸®´Â ¼ºí¸´À» À§ÇÑ ³í¸®ÀûÀÎ µð·ºÅ丮ÀÌ´Ù. ÀÌ°ÍÀº zone ¿Í ¿¬°áµÇ¾îÁö´Âµ¥ zoneÀº URL¿¡ ÷°¡µÇ¾îÁø´Ù. ¸¸¾à zone ÀÇ À̸§ÀÌ servlet ÀÌ°í ¾Æ·¡ÀÇ ¿¹Á¦¼Ò½º¸¦ ÄÄÆÄÀÏ ÈÄ ÁöÁ¤µÈ ¸®Æ÷ÁöÅ͸®¿¡ ÀÖ´Ù¸é ½ÇÇàÀ» À§ÇØ ºê¶ó¿ìÁ®ÀÇ URL â¿¡ ´ÙÀ½Ã³·³ ÀÔ·ÂÇØÁÖ¸é µÇ´Âµ¥ Àڽſ¡°Ô ¸Â°Ô ¹Ù²Ù¾îÁÖ¸é µÈ´Ù.
http://localhost/servlet/Hello
ȤÀº
http://210.110.144.162/servlet/Hello
ȤÀº
http://nogadax.tit.ac.kr/servlet/Hello
´ÙÀ½Àº "Hello.java" ¶ó´Â Å×½ºÆ®¿ëÀÇ ¼Ò½ºÀÌ´Ù.
-------------------------------
import java.io.*;
import javax.servlet.*;
import java.sql.*;
import java.text.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet
{
Connection db;
Statement st;
PrintWriter out;
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String title = "Example Apache JServ Servlet";
response.setContentType("text/html");
out = response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">");
out.println("<H1>" + title + "</H1>");
out.println("<H2>Congratulations, Apache JServ is working!</H2>");
try {
test();
} catch(Exception ex){
out.println("<h2>DB error!</h2>");
}
out.println("</BODY></HTML>");
out.close();
}
public void cleanup()
{
try {
st.executeUpdate("drop table basic");
} catch(Exception ex) { }
}
public void doexample() throws SQLException
{
out.println("<br><h2>Running tests:</h2>");
st.executeUpdate("create table basic (a int2, b int2)");
st.executeUpdate("insert into basic values (1,1)");
st.executeUpdate("insert into basic values (2,1)");
st.executeUpdate("insert into basic values (3,1)");
st.executeUpdate("update basic set b=8");
out.println("<h2>Updated "+st.getUpdateCount()+" rows</h2>");
PreparedStatement ps = db.prepareStatement("insert into
basic values (?,?)");
for(int i=2;i<5;i++) {
ps.setInt(1,4); // "column a" = 5
ps.setInt(2,i); // "column b" = i
ps.executeUpdate();
}
ps.close();
out.println("<h2>performing a query</h2>");
ResultSet rs = st.executeQuery("select a, b from basic");
if(rs!=null) {
while(rs.next()) {
int a = rs.getInt("a");
int b = rs.getInt(2);
out.println("<h2> a="+a+" b="+b+"</h2>");
}
rs.close();
}
}
public void test()
throws ClassNotFoundException, FileNotFoundException,
IOException,SQLException
{
String url = "jdbc:postgresql:nogadax";
String usr = "postgres";
String pwd = "";
Class.forName("postgresql.Driver");
db = DriverManager.getConnection(url, usr, pwd);
out.println("<h2>Connecting to Database URL = " + url +"</h2>");
out.println("<h2>Connected...Now creating a statement</h2>");
st = db.createStatement();
cleanup();
doexample();
cleanup();
out.println("<h2>Now closing the connection</h2>");
st.close();
db.close();
}//end test
}
½ÇÇà °á°ú
½ÇÇà °á°ú´Â ´ÙÀ½°ú °°´Ù. ¾Æ·¡¿¡¼ º¸¿©Áö´Â URLÀÌ ´Ù¸¦ ¼ö ÀÖ´Ù.
Example Apache JServ Servlet
Congratulations, Apache JServ is working!
Connecting to Database URL = jdbc:postgresql:nogadax
Connected...Now creating a statement
Running tests:
Updated 3 rows
performing a query
a=1 b=8
a=2 b=8
a=3 b=8
a=4 b=2
a=4 b=3
a=4 b=4
Now closing the connection