Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


MySQL JDBC Connection
 
ÇÁ·Î±×·¡¹Ö¿¡ À־ µ¥ÀÌÅͺ£À̽º´Â ¾ø¾î¼­´Â ¾ÈµÉ µµ±¸ÀÌ´Ù. ÀÚ¹Ù´Â À¯¿¬ÇÑ JDBC¸¦ Á¦°øÇÑ´Ù. ÀÌ JDBC¸¦ ÅëÇؼ­ MySQLÀ» Á¢¼ÓÇÏ¿© µ¥ÀÌÅ͸¦ °¡Á®¿Àµµ·Ï ÇÏÀÚ ( 2003/02/28 ) 554
Written by ienvyou - ÃÖÁö¿õ
2 of 3
 
ÇÊ¿äÇÑ TABLEÀ» ¸¸µå´Â ¼ø¼­:
1. Database¸¸µé±â 
2. MySQL¿ë »ç¿ëÀÚ ¸¸µé±â
3. Table¸¸µé±â

- javabrain¿ë DB ¸¸µé±â(2°¡Áö ¹æ¹ý)
1.UNIX shell»ó¿¡¼­ ¸¸µé±â 
# mysqladmin -u root -p create javabrain

2.DB¿¡ loginÈÄ ¸¸µé±â
mySQL¿¡¼­Á¦°øÇÏ´Â ¸í·É¾îÁß mysqladminÀº °ü¸®¿ë
mysqlÀº client¿ë ¸í·É¾î.

# mysql -u root -p mysql
mysql> create database javabrain;
 
** mysql ¸í·É¾î ¿ä¾à
mysql [-h DB¼­¹öIP] [-u DB¿ë»ç¿ëÀÚID] [-p[¾ÏÈ£]] [Database] [< sql-scriptÆÄÀÏ ]
-p ¸¸ ºÎ¿©ÇÏ¸é  ¾ÏÈ£´Â ¹°¾îº½. ¾ÏÈ£±îÁö ÁÖ·Á¸é -p¾ÏÈ£(-p¿Í ¾ÏÈ£¸¦ ºÙ¿©¾¸)


- »õ»ç¿ëÀÚµî·Ï
¸ÕÀú MySQL¿¡ root·Î login
# mysql -u root -p mysql

mysql> grant SELECT,insert,update,delete,create,drop
 > on javabrain.*
 > to scott@'localhost'
 > identified by 'tiger';
mysql> grant SELECT,insert,update,delete,create,drop
 > on javabrain.*
 > to scott@'%'
 > identified by 'tiger';
mysql> flush privileges;

¶Ç´Â

mysql> grant all privileges 
 > on javabrain.* to scott@'%' 
 > identified by 'tiger';

mysql> grant all privileges 
 > on javabrain.* to scott@localhost 
 > identified by 'tiger'; 
mysql> flush privileges;

scott »ç¿ëÀÚ¸¦ µî·ÏÇÒ¶§ ¿©·¯°¡Áö(3°¡Áö) ¹æ¹ýÀÌ Àִµ¥ ±×Áß Çϳª¸¦ ¾¸
µî·Ï½Ã ¹Ýµå½Ã to scott@'localhost'¿Í to scott@'%'¸¦ µÑ´Ù ÇØÁà¾ßÇÔ
localhost¿Í %´Â Á¢¼ÓÀ» Çã¿ëÇÏ´Â hostnameÀε¥ localhost¸¦ »©¸é ´Ù¸¥ ½Ã½ºÅÛ¿¡¼­ 
networkÀ¸·Î¸¸ Á¢±ÙÇÒ ¼ö ¹Û¿¡ ¾øÀ½. 
±×¸®°í %¶õ ¸ðµçÈ£½ºÆ®¸¦ ÀǹÌÇϱ⶧¹®¿¡ %¸¦ ÁöÁ¤ÇؾßÁö¸¸ ´Ù¸¥ ½Ã½ºÅÛ¿¡¼­ 
networkÀ¸·Î MySQL·Î Á¢±Ù°¡´É( mysql -h hostIP¶Ç´Âhostname -u »ç¿ëÀÚID -p )

user»ý¼ºÈÄ ¹Ýµå½Ã DB¸¦ reload Çؾ߸¸ ¸¸µç »ç¿ëÀÚ°¡ Á¢¼Ó°¡´É(¾Æ·¡¸í·É¾î)

# mysqladmin -u root -p reload
¶Ç´Â
mysql> flush privileges;


Á¢¼ÓÇؼ­ È®ÀÎÇϱâ 
# mysql -u scott -p javabrain  --> ¤ÔDB¼­¹ö¿¡¼­login

´Ù¸¥½Ã½ºÅÛ¿¡¼­
# mysql -h DB¼­¹öÀÇIPÁÖ¼Ò -u scott -p javabrain


ÇÊ¿äÇÑ Å×ÀÌºí ¸¸µé±â(customer,shares,stock) 
-ÁÖÀÇ»çÇ×: mysqlÀº table name¿¡¼­ÀÇ ´ë¼Ò¹®ÀÚ¸¦ ±¸ºÐÇÔ
(ÀÌ°ÍÀº MySQLÀÌ ¼³Ä¡µÈ OS¸¦ µû¸§. Áï M$°è¿­¿¡¼­´Â ±¸ºÐ¾ÈÇÔ)

ÇÊ¿äÇÑ sql¹®À» ÆÄÀÏ(maketable.sql-È®ÀåÀÚ´Â ¹«°ü)·Î ¸¸µçÈÄ shell»ó¿¡¼­ ¾Æ·¡
¸í·É¾î ½ÇÇà

# mysql -u scott -p javabrain < maketable.sql

È®ÀÎ
# mysql -u scott -p javabrain
mysql> show tables;
mysql> select * from customer;
mysql> select * from shares;
mysql> select * from stock;

- JDBC Driver »ç¿ëÇϱâ
MySQL¿ë JDBC Driver´Â 2°¡Áö°¡ ÀÖÁö¸¸ ¿©±â¼­´Â mm driver¸¦ »ç¿ë.

http://www.worldserver.com/mm.mysql/ ¿¡ °¡¼­ ÃֽŠ¹öÀüÀ» downloadÇÒ°Í
(mm.mysql-2.0.4-bin.jar)

°¡Á®¿Â°ÍÀ» Àû´çÇÑ °÷¿¡¼­ ÇØÁ¦.

»ç¿ë¹ý:

Class.forName("org.gjt.mm.mysql.Driver");
String url="jdbc:mysql://127.0.0.1:3306/javabrain";
Connection conn = DriverManager.getConnection(url,"scott","tiger");

 

°ü·Ã ¸í·É¾îµé.

# mysql -u root -p mysql
password: 
mysql> select user();               --> ÇöÀç ¾î¶² user±ÇÇÑÀ¸·Î Á¢¼ÓÇÏ°í ÀÖ´ÂÁö.
mysql> show database;               -->µ¥ÀÌÅͺ£À̽º Á¾·ùº¸±â
mysql> show tables from mysql;      --> mysql»óÀÇ Å×À̺í Á¤º¸º¸±â
mysql> show columns from customer;  --> customer Å×ÀÌºí ±¸Á¶º¸±â
mysql> show index from customer;    --> customerÅ×À̺íÀÇ À妽ºº¸±â
mysql> use mysql;                   --> »ç¿ëÇÒ database¸¦ º¯°æ
(±âº»ÀûÀ¸·Î MySQL¿¡´Â µÎ°³ÀÇ database-mysql,test-°¡ ÀÖÀ½.
  mysqlÀº DBMS°ü¸®¿ë Á¤º¸, test´Â ¿¬½À¿ë
  ±×·¡¼­ mysqlÀ̶õ database´Â »ç¿ë½Ã ÁÖÀÇÇÒ°Í)
 

#########################################
#########################################
##
##   Ãß °¡ »ç Ç×
##
#########################################
#########################################

javabrain.sql À̶ó´Â ÆÄÀÏÀ» ¼öÇà ½ÃÅ°¸é SL-300À»À§ÇÑ DB¸¦ À§ÇÑ ¸ðµç ³»¿ëÀÌ ÀÚµ¿ ¼³Á¤µÊ.

»ç¿ë¿¹)

mysql ¼­¹ö¸¦ °¡µ¿½ÃŲÈÄ ( mysqld.exe )
prompt> mysql -u root -p <  javabrain.sql

ÀÌ·¯¸é ¾Æ·¡ »çÇ×ÀÌ ÀÚµ¿ »ý¼ºµÊ 
databaseÀ̸§  : javabrain
root¾ÏÈ£   : javabrain
»ç¿ëÀÚ id  : scott
¾ÏÈ£    : tiger
»ý¼ºµÈtableµé  : customer, shares, stock

°¡²û¾¿ userºÎºÐÀÌ Àû¿ëÀÌ ¾ÈµÇ´Â °æ¿ì¿¡´Â DB¸¦ restart½ÃÅ°¸é µÈ´Ù.

prompt> mysqladmin -u root -p shutdown   --> DB shutdown
prompt> mysqld

¸¸¾à ¾ÏÈ£¸¦ ¹Ù²Ù°í ½ÍÀ¸¸é  sql script³»ºÎ¿¡  .... identified by ´ÙÀ½ºÎºÐÀÌ ¾ÏÈ£ÀÌ´Ï
¾Ë¾Æ¼­ ¹Ù²Ù¸é µÊ.  ´Ü ¹Ýµå½Ã ¾ÏÈ£´Â single quotationÀ¸·Î ¹­¾îÁÙ°Í.

¿¹) ¾ÏÈ£¸¦ apple ·Î ÇÏÀÚ¸é  .... identified by 'apple'

 
 
1 2 3
References
 
http://www.mysql.com
http://java.sun.com/jdbc
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD