Search Java Code Snippets

  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.

Java - Code Samples

 Sample 1. Database Api using java.sql classes

package BuggyBread;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseAPI {

   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "";

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

   Connection conn = null;
   public void initializeConnection(){
      try {
         // STEP 2: Register JDBC driver

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

      } catch (SQLException se) {
         // Handle errors for JDBC
      } catch (Exception e) {
         // Handle errors for Class.forName
      } finally {
         try {
            if (conn != null)
         } catch (SQLException se) {
         }// end finally try
      }// end try

   Like      Feedback     database api  java.sql

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner