skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 6, 2012

Connecting Sybase using JDBC

Posted by Raju Gupta at 12:38 AM – 0 comments
 
This is a java code snippet which can be used to connect Sybase database using JDBC driver (SybDriver). Jconnect is the name of the package which contains the JDBC driver. You can use the jdbc driver provided by Sybase. Using Jconnect we are going to connect a sybase database
import java.sql.*;
import com.sybase.jdbcx.*;

public class SybaseConnectivity {
 Connection con = null;
 Statement stmt = null;
 SybDriver sybDriver = null;

 public void makeConnection() throws Exception {
  // try
  {
   sybDriver = (SybDriver) Class.forName(
     "com.sybase.jdbc3.jdbc.SybDriver").newInstance();
   System.out.println("Driver loaded");
   con = DriverManager
     .getConnection("jdbc:sybase:Tds:<server ip><port>",
       "username", "password");

   Statement stmt = con.createStatement();

   // Execute the query
   ResultSet rs = stmt.executeQuery("select * from test");

   // Loop through the result set
   while (rs.next()) {
    System.out.println(rs.getString(1));
   }

   // Close the result set, statement and the connection
   rs.close();
   stmt.close();
   con.close();

  }// catch(Exception e)
  {
   // System.out.println("Error is "+e.getMessage());
  }
 }

 public static void main(String args[]) throws Exception {
  SybaseConnectivity sc = new SybaseConnectivity();
  sc.makeConnection();
 }
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger