Catalysoft   Turtle
home products articles about us contact us

What is 'JDBC'?

JDBC is the Java DataBase Connectivity Framework. It is the most common way for Java to communicate directly with a database, and Java enterprise solutions depend on it.

Here is some sample code that accesses a MySQL database to execute a query and print the results. Note that the program currently assumes that the database server is running on the same machine as it will be run on, as the database URL makes reference to the localhost machine. In practice, such details should be held in a configuration file, and passwords should either be accepted from the user at run-time, or encrypted if stored.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
    public static final String driverClassName = "org.gjt.mm.mysql.Driver";
    public static final String sourceURL = "jdbc:mysql://localhost/Menagerie";
    public static final String user="<user>";
    public static final String password="<password>";
    public static String sql = "SELECT owner, name from pet order by owner";
    
    public static void main(String[] args) {
        try {
            Class.forName(driverClassName).newInstance();
            Connection connection = DriverManager.getConnection(sourceURL, user, password);
            Statement statement = connection.createStatement();
            ResultSet result = statement.executeQuery(sql);
            printResults(result);
            connection.close();
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace(System.out);
        } catch (InstantiationException ie) {
            ie.printStackTrace(System.out);
        } catch (IllegalAccessException iae) {
            iae.printStackTrace(System.out);
        } catch (SQLException sqle) {
            sqle.printStackTrace(System.out);
        }
    }
    
    public static void printResults(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        printMetaData(metaData);
        int numColumns = metaData.getColumnCount();
        while (rs.next()) {
            for (int i=1; i<=numColumns; i++) {
                System.out.print(rs.getString(i)+" ");
            }
            System.out.println();
        }
    }
    
    public static void printMetaData(ResultSetMetaData metaData) throws SQLException {
        for (int i=0; i<metaData.getColumnCount(); i++) {
            System.out.print(metaData.getColumnName(i+1)+" ");
        }
        System.out.println();
        System.out.println("-------------------------------------------------------------");
    }
}

Other Terms

A9AlgernonantAOPArtificial Intelligence
awkBeanShellCCamel CaseCastor
Cladonia Exchanger XML EditorCLISPcollabetitioncygwinDAO
DOMEclipseEditiXErlangExpert System
Extreme ProgrammingFirefoxfirst class objectFortranFreeMind
GroovyHaskellHIBERNATEHSQLIDE
JathaJavaJAXBjBPMJENA
JESSJRulesJUnitJythonKerberos
LISPMalwareOMGPrologProtege
PythonRDFSESAMESquiggleSUMO
SwoogleTrojan HorseXML-Java BindingXMLSpy