::: 강좌/소스/문서 :::

강좌/소스/문서 성격에 맞지 않는 광고,비방,질문의 글은 즉시 삭제하며
내용을 복사하여 사용할 경우 반드시 이곳(http://www.howto.pe.kr)을 출처로 명시하여 주세요


Category

  신훈재(2004-06-18 13:44:27, Hit : 6420, Vote : 1457
 [자바] 여러종류의 데이타베이스 테스트 소스

[ 여러종류의 데이타베이스 테스트 소스 ]

---------------------------------------------------------------------------------
/*
ODBC 시스템 설정:
제어판->ODBC->시스템 DSN->추가->Driver do Microsoft Access (*.mdb)->마침->
데이타 원본이름: url의 연결 DB명 "testDB"->만들기->testDB.mdb->확인->확인->확인
*/
import java.sql.*;

public class Test
{
        public static void main ( String args [] )
        {
                String driver = "sun.jdbc.odbc.JdbcOdbcDriver";//ODBC
                //String driver = "oracle.jdbc.driver.OracleDriver";//Oracle
                String url = "jdbc:odbc:testDB";//ODBC. testDB ODBC 연결명
                //String url = "jdbc:oracle:thin:@192.168.1.103:1521:ORCL";//Oracle thin. @ip 혹은 리스너명:포트:SID
                //String url = "jdbc:oracle:oci8:@";//Oracle oci
                //String url="jdbc:msql://203.253.23.3:1114/mydb";//miniSQL
                //String url = "jdbc:postgres95:mydb";//Postgres
                String id = "scott";
                String password = "tiger";

                try//드라이버 로드
                {
                        Class.forName ( driver );
                }           
                catch ( Exception e ) { System.out.println ( "1: Driver Fail..." ); return; }
        
                Connection con = null;
                try//디비 접속
                {
                        con = DriverManager.getConnection ( url, id, password );
                        
                        DatabaseMetaData dbMeta = con.getMetaData ( );//데이타베이스 정보 추출
                        
                        System.out.println ( "ProductName: " + dbMeta.getDatabaseProductName ( ) );
                        System.out.println ( "ProductVersion: " + dbMeta.getDatabaseProductVersion ( ) );
                        System.out.println ( "DriverName: " + dbMeta.getDriverName ( ) );
                        System.out.println ( "DriverVersion: " + dbMeta.getDriverVersion ( ) );
                        System.out.println ( "URL: " + dbMeta.getURL ( ) );
                        System.out.println ( );
                }           
                catch ( Exception e ) { System.out.println ( "2: Connect Fail..." ); return; }

                Statement sm = null;    
                try//쿼리 수행
                {
                        sm = con.createStatement ( );
                        
                        sm.executeUpdate ( "drop table test" );//테이블 삭제. 테이블이 없으면 다음부터
                        
                        sm.executeUpdate ( "create table test ( user varchar (20), password varchar (20) ) ");//테이블 생성

                        sm.executeUpdate ( "insert into test values ( 'yuksamgu', '6390' )" );//데이타 입력
                        
                        sm.executeUpdate ( "update test set password='639' where user='yuksamgu'" );//데이타 변경
                        
                        ResultSet rs = sm.executeQuery ( "select * from test" );//데이타 추출
                        
                        ResultSetMetaData rsMeta = rs.getMetaData ( );//필드 정보 추출
                        
                        int columnType = 0; String columnTypeName = "", columnLabel = "";
                        for ( int i = 1; i <= rsMeta.getColumnCount ( ); i++ )//i=1과 <=에 주의
                        {
                                columnType = rsMeta.getColumnType ( i );
                                columnTypeName = rsMeta.getColumnTypeName ( i );
                                columnLabel = rsMeta.getColumnLabel ( i );//필드 라벨명
                                
                                System.out.print ( columnLabel + "(" + columnType + "," + columnTypeName + ")" + "\t" );
                        }
        
                        System.out.println ( );
                        
                        String field = "";
                        while ( rs.next ( ) )//레코드 위치 증가
                        {
                                for ( int i = 1; i <= rsMeta.getColumnCount ( ); i++ )
                                {
                                        field = format ( rs, rsMeta.getColumnType ( i ), i );
                                        
                                        //field = rs.getString ( i );//i 번째 필드 추출
                                        
                                        System.out.print ( field + "\t\t" );
                                }
                                
                                System.out.println ( );
                        }
                }
                catch ( Exception e ) { System.out.println ( "3: SQL Fail..." ); return; }
                        
                try//닫기
                {                
                        sm.close ( );
                        con.close ( );
                }           
                catch ( Exception e ) { System.out.println ( "4: Close Fail..." ); return; }
        }

        private static String format ( ResultSet rs, int dataType, int col ) throws SQLException
        {
                switch ( dataType )
                {
                        case Types.DATE:
                                java.sql.Date date = rs.getDate(col);
                                return date.toString();
                        case Types.TIME:
                                java.sql.Time time = rs.getTime(col);
                                return time.toString();
                        case Types.TIMESTAMP:
                                java.sql.Timestamp timestamp = rs.getTimestamp(col);
                                return timestamp.toString();
                        case Types.CHAR:
                        case Types.VARCHAR:
                        case Types.LONGVARCHAR:
                                String str = rs.getString(col);
                                return str;
                        case Types.NUMERIC:
                        case Types.DECIMAL:
                                java.math.BigDecimal numeric = rs.getBigDecimal(col, 10);
                                return numeric.toString();
                        case Types.BIT:
                                boolean bit = rs.getBoolean(col);
                                return String.valueOf ( new Boolean ( bit ) );
                        case Types.TINYINT:
                                byte tinyint = rs.getByte(col);
                                return String.valueOf ( new Integer ( tinyint ) );
                        case Types.SMALLINT:
                                short smallint = rs.getShort(col);
                                return String.valueOf ( new Integer ( smallint ) );
                        case Types.INTEGER:
                                int integer = rs.getInt(col);
                                return String.valueOf ( new Integer ( integer ) );
                        case Types.BIGINT:
                                long bigint = rs.getLong(col);
                                return String.valueOf ( new Long ( bigint ) );
                        case Types.REAL:
                                float real = rs.getFloat(col);
                                return String.valueOf ( new Float ( real ) );
                        case Types.FLOAT:
                        case Types.DOUBLE:
                                double longreal = rs.getDouble(col);
                                return String.valueOf ( new Double ( longreal ) );
                        case Types.BINARY:
                        case Types.VARBINARY:
                        case Types.LONGVARBINARY:
                                byte[] binary = rs.getBytes(col);
                                return new String ( binary, 0 );
                        default:
                                return "";
                }
        }
}

/*
결과:
ProductName: ACCESS
ProductVersion: 04.00.0000
DriverName: JDBC-ODBC Bridge (ODBCJT32.DLL)
DriverVersion: 2.0001 (04.00.4403)
URL: jdbc:odbc:testDB

user(12,VARCHAR)        password(12,VARCHAR)
yuksamgu                    639
*/
---------------------------------------------------------------------------------







23   [Java/PHP] [자바] DB 컨넥션 풀  신훈재 2004/06/18 9511 1857
22   [Java/PHP] [자바] JDBC-ODBC 연결 예제  신훈재 2004/06/18 9046 1672
21   [Java/PHP] [자바] 시스템 프로퍼티를 이용한 드라이버 로드  신훈재 2004/06/18 5487 1425
20   [Java/PHP] [자바] 오라클 각종 에러  신훈재 2004/06/18 6489 1671
19   [Java/PHP] [자바] 활용팁(오라클)  신훈재 2004/06/18 5997 1603
18   [Java/PHP] [자바] ResultSet 질의결과  신훈재 2004/06/18 6111 1317
17   [Java/PHP] [자바] Select예제  신훈재 2004/06/18 6249 1562
16   [Java/PHP] [자바] Update예제  신훈재 2004/06/18 5414 1333
15   [Java/PHP] [자바] Insert 예제  신훈재 2004/06/18 6492 1673
14   [Java/PHP] [자바] Query Statement  신훈재 2004/06/18 7763 1468
13   [Java/PHP] [자바] JDBC Connect  신훈재 2004/06/18 6717 1489
12   [Java/PHP] [자바] JDBC 코딩시 주위사항(jsp, servlet)  신훈재 2004/06/18 6137 1357
  [Java/PHP] [자바] 여러종류의 데이타베이스 테스트 소스  신훈재 2004/06/18 6420 1457
10   [Java/PHP] [자바] JDBC 테스트 소스(오라클)  신훈재 2004/06/18 5436 1345
9   [Java/PHP] [소스] snmp 를 이용한 시스템 가용율 구하기 예제  김영대 2003/12/01 9569 2092
8   [Java/PHP] [소스] Oolong 언어를 이용한 Factorial 계산 프로그램 예제  김영대 2003/11/10 6633 1683
7   [Java/PHP] [문서] Java Web Services Development Pack (JWSDP) 1.0 tutorial  김영대 2003/03/24 5851 1457
6   [Java/PHP] [문서] Java Swing v1.1.1 도움말  김영대 2003/03/24 5782 1470
5   [Java/PHP] [문서] Java Servlet Development Kit (JSDK) v2.3 도움말  김영대 2003/03/24 5520 1454
4   [Java/PHP] [문서] Java 2 SDK, Enterprise Edition (J2EE) 1.3.1 도움말  김영대 2003/03/24 5617 1469

1 [2]
 

Copyright 1999-2017 Zeroboard / skin by zero