본문 바로가기

DBMS/sybase

Sybase IQ jdbc connect

원본 : http://etoil.tistory.com/66




#### server.xml  ############
  
  <Context crossContext="true" debug="0" docBase="KSFC" path="/KSFC" reloadable="false">
  <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_ksfc_log." suffix=".txt" timestamp="true"/>
  <Resource auth="Container" name="jdbc/KSFC" type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/KSFC">
    <parameter>
          <name>factory</name>
          <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
       </parameter>
       <parameter>
         <name>username</name>
         <value>id_name</value>
       </parameter>
       <parameter>
         <name>password</name>
         <value>pwd</value>
       </parameter>
       <parameter>
          <name>driverClassName</name>
          <value>com.sybase.jdbc3.jdbc.SybDriver</value>
       </parameter>
       <parameter>
         <name>url</name>
        <value>jdbc:sybase:Tds:127.0.0.1:3638?ServiceName=DBEAP</value>
       </parameter>
       </ResourceParams>
    </Context>


########## web.xml ##############

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- db pooling descriptors -->
 <resource-ref>
  <description>DB connection</description>
  <res-ref-name>jdbc/KSFC</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref> 



####### connection.jsp #################

<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>

<%
 Context initContext = null;
 Context envContext = null;
 DataSource ds = null;

 Connection conn = null;
 java.sql.Statement stmt = null;
 ResultSet rs= null;


try
{
 initContext = new InitialContext();
 envContext = (Context) initContext.lookup("java:comp/env"); 
 ds = (DataSource)envContext.lookup("jdbc/KSFC");

System.out.println("1.....");
 conn = ds.getConnection();
System.out.println("2.....");

%>
<html><head><title>Untitled Document</title></head>
<body>

<% 
 stmt = conn.createStatement();
 String getDateSQL = "";
 getDateSQL = "Select 1 ";

 rs = stmt.executeQuery(getDateSQL);

int start = 0 ;

 while(rs.next()) {
  start =rs.getInt(1);
  out.println(start);
 }
 rs.close();
%>
</body>
</html>
<% 
 stmt.close();
}catch(Exception e){
 out.println(e);
}finally{
 try{if(stmt != null) stmt.close();} catch(Exception e){}
 try{if(conn != null) conn.close();} catch(Exception e){}
}
%> 





################################################
   jdbc 직접연결시...
################################################

<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*,java.util.*,com.sybase.jdbcx.*"%> 

<%

 Context initContext = null;
 Context envContext = null;
 DataSource ds = null;
 Connection conn = null; //Connection Class 선언
 ResultSet rs= null;

 Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
 
 String query = "select 1 ";
 
 try {
  conn = DriverManager.getConnection("jdbc:sybase:Tds:127.0.0.1:3638/DB", "sa", "1234");
  System.out.println( conn ) ;
 }
 catch (Exception e) {
  System.out.println("Connection error: " + e.toString());
 }

 java.sql.Statement stmt = null;
 stmt = conn.createStatement();

 rs = stmt.executeQuery(query);
       
 try {
 
  while (rs.next()) {
   System.out.println("step05");
   System.out.println("code:" + rs.getInt(1) );
  }

%>

 


<%


 }
 catch (Exception e) {
  e.printStackTrace();
 }
 finally {
  try {
   conn.close();
  }
  catch (Exception e) {}
 }
%>