user3435220
user3435220

Reputation: 31

import com.sybase.jdbc4.jdbc.*; error

I am writing my first java program that should connect to the database. I have successfully created the JDBC driver (version 4.0) and it pings successfully. Next, I am trying below code and it doesnt work. I found that i need to import com.sybase.*; for that. which gives compile error stating the import cannot be resolved. (I am very new to java.. this is my first program so any help is greatly appreciated). Thanks. I am using sybase 15.x.

enter code here

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.sybase.jdbc4.jdbc.*;    --> this is where I see error. 


public class DBConnTest {
       // JDBC driver name and database URL
       //static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        //static final String DB_URL = "jdbc:mysql://localhost/EMP";
        static final String JDBC_DRIVER = "Sybase15";
        static final String DB_URL = "jdbc:sybase:Tds:<IP>:<PORT>/<DB>?CHARSET=iso_1";

       //  Database credentials
       static final String USER = "abc";
       static final String PASS = "def";

       public static void main(String[] args) {
       Connection conn = null;
       Statement stmt = null;
       try{
          //STEP 2: Register JDBC driver
          //Class.forName("com.sybase.jdbc4.jdbc.SybDriver");
           Class.forName("com.sybase.jdbc4.jdbc.SybDriver");

          //STEP 3: Open a connection
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL,USER,PASS);

          //STEP 4: Execute a query
          System.out.println("Creating statement...");
          stmt = conn.createStatement();
          String sql;
          sql = "SELECT ID,AGE,FIRST,AST FROM testtbl";
          ResultSet rs = stmt.executeQuery(sql);

          //STEP 5: Extract data from result set
          while(rs.next()){
             //Retrieve by column name
             int id  = rs.getInt("ID");
             int age = rs.getInt("AGE");
             String first = rs.getString("FIRST");
             String last = rs.getString("LAST");

             //Display values
             System.out.print("ID: " + id);
             System.out.print(", AGE: " + age);
             System.out.print(", FIRST: " + first);
             System.out.println(", LAST: " + last);
          }
          //STEP 6: Clean-up environment
          rs.close();
          stmt.close();
          conn.close();
       }catch(SQLException se){
          //Handle errors for JDBC
          se.printStackTrace();
       }catch(Exception e){
          //Handle errors for Class.forName
          e.printStackTrace();
       }finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
       System.out.println("Goodbye!");
    }//end main
    }//end FirstExample

Upvotes: 2

Views: 7395

Answers (1)

user3435220
user3435220

Reputation: 31

So I got this part working. I had to put jconn4.jar from C:\Sybase\jconnect-7\classes (this is where I had Sybase components installed) to where my java code resides under: C:\Program Files\Java\jdk1.6..\jre\lib and now i am able to connect and query the database. Once this is done, there is no need to put the import com.sybase statement in the imports.

I still have one more question. The way above code is written, the IP, Port, userid, password everything is hardcoded in the .java file. However I had inserted all these information while using the JDBC Driver "Sybase15" in above code.

How can I write a code that directly use the driver and eliminate the need for putting IP/PORT, userid/password kind of sensitive information? This will have few key benefits:

  1. if the driver info changes, I can only update the driver and code still works and
  2. userid/password info need not be embedded in the code. so security increase, also multiple people can use the same .java source code by configuring their own JDBC connectivity.

Any help is greatly appreciated. I am very new to Java, so I apologize if this was a dumb question. I got this above code from internet and modified it to my purpose. Thanks a lot.

Upvotes: 1

Related Questions