JAVA DB 연결 하기
1.odbc인 경우
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jbdc:odbc:FirstDB");
conn=DriverManager.getConnection("jbdc:odbc:DNSname","id","pass");
▒▒ 연결셋팅 ▒▒
시작→제어판→관리도구→데이터원본→시스템DSN→추가→microsoft access driver선택
2.oracle인경우
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:system_id","scott",tiger");
▒▒ 연결셋팅 ▒▒
★윈도우환경셋
시작→제어판→관리도구→데이터원본→시스템DSN→추가→microsoft ODBC for Oracle선택
★직접셋팅
C:\oracle\product\10.2.0\db_1\jdbc\lib 폴더의 ojdbc14.jar 복사해서
C:\jdk1.6\jre\lib\ext 와 C:\jre1.6\lib\ext 에 복사
★mysql connector설치
홈페이지(www.mysq.com)에서 mysql-connector-java-5.1.0.zip다운후 압축풀고
mysql-connector-java-5.1.0-bin.jar 파일을 C:\jdk1.6\jre\lib\ext 와
C:\jre1.6\lib\ext 에 복사
★입클립스 셋팅
Window→reperences→installed JREs에서 jre1.6선택후 Edit클릭→JRE home directory에서 위에서 복사한
C:\oracle\product\10.2.0\db_1\jdbc\lib 폴더의 ojdbc14.jar선택→OK
★Db 이름 찾기
C:\oracle\product\10.2.0\db_1\NETWORK\ADMIN폴터의 tnsnames.ora파일을 열어서확인
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 702E3)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl) <--요기 orcl이 이름
)
)
3.mysql인경우
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root",kbj002");
Class.forName("com.mysql.Driver");
String url="jdbc:mysql://localhost:3306/test";
String id="root";
String pwd="";
con=DriverManager.getConnection(url,id,pwd);
---------------------------- Odbc 예 제 -----------------------------------------
import java.sql.*;
public class DriverTest{
public static void main(String grgs[]){
Connection con;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:jdbc","java","java");
System.out.println("Success!");
}
catch(SQLException ex){System.out.println("SQLException:"+ex);}
catch(Exception ex){System.out.println("Exception:"+ex);}
}
}
----------------------------odbc 예 제 -----------------------------------------
import java.io.*;
import java.sql.*;
class JDBC_Select{
public static void main(String grgs[]){
String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String url="jdbc:odbc:jdbc";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String sql="select * from member";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,"java","java");
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
System.out.println("ID1\t\t id\t\t pass\t\t name\t\t e_mail\t\t\t\t phone\t\t zipcode\t job");
System.out.println("-------------------------------------------------------------------------------------------");
while(rs.next()){
System.out.print(rs.getLong("ID1")+"\t\t");
System.out.print(rs.getString("id")+"\t\t");
System.out.print(rs.getString("pass")+"\t\t");
System.out.print(rs.getString("name")+"\t\t");
System.out.print(rs.getString("e_mail")+"\t\t");
System.out.print(rs.getLong("phone")+"\t\t");
System.out.print(rs.getString("zipcode")+"\t\t");
System.out.println(rs.getString("job")+"\t\t");
}
}catch(Exception e){
System.out.println("데이터 베이스 연결 실패");
}
finally{
try{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(con!=null)con.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
----------------------------odbc 예 제 -----------------------------------------
import java.io.*;
import java.sql.*;
class JDBC_Select1{
public static void main(String grgs[]){
String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String url="jdbc:odbc:jdbc1";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String sql="select * from member";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,"java","java");
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
ResultSetMetaData rsm=rs.getMetaData();
int cols=rsm.getColumnCount();
for (int i=1; i<=cols; i++){
System.out.print(rsm.getColumnName(i)+"\t\t");
}
System.out.println();
while(rs.next()){
for(int i=1; i<=cols; i++){
System.out.print(rs.getObject(i)+"\t\t");
}
System.out.println();
}
}catch(Exception e){
System.out.println("데이터 베이스 연결 실패");
}
finally{
try{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(con!=null)con.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
---------------------------- Oracle 예 제 -----------------------------------------
import java.io.*;
import java.sql.*;
class JDBC_Select2{
public static void main(String grgs[]){
String driver ="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String sql="select * from emp";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,"scott","tiger");
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
ResultSetMetaData rsm=rs.getMetaData();
int cols=rsm.getColumnCount();
for (int i=1; i<=cols; i++){
System.out.print(rsm.getColumnName(i)+"\t\t");
}
System.out.println();
while(rs.next()){
for(int i=1; i<=cols; i++){
System.out.print(rs.getObject(i)+"\t\t");
}
System.out.println();
}
}catch(Exception e){
System.out.println("데이터 베이스 연결 실패");
}
finally{
try{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(con!=null)con.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
---------------------------- MySql 예 제 -----------------------------------------
import java.io.*;
import java.sql.*;
class JDBC_Select3{
public static void main(String grgs[]){
String driver ="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/mysql";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String sql="select * from personal";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,"root","");
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
ResultSetMetaData rsm=rs.getMetaData();
int cols=rsm.getColumnCount();
for (int i=1; i<=cols; i++){
System.out.print(rsm.getColumnName(i)+"\t\t");
}
System.out.println();
while(rs.next()){
for(int i=1; i<=cols; i++){
System.out.print(rs.getObject(i)+"\t\t");
}
System.out.println();
}
}catch(Exception e){
System.out.println("데이터 베이스 연결 실패");
}
finally{
try{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(con!=null)con.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}