Professional Documents
Culture Documents
JDBC Programming Examples
JDBC Programming Examples
JDBC Programming Examples
1. Substitute your driver's JDBC URL for the generic JDBC URL
that appears in the code. In other words, put your driver's JDBC URL
between the quotation marks in the follwoing line:
The documentation for your driver should give you this URL.
Class.forName("myDriver.ClassName");
3. Substitute the username and password you use for your database
in the following:
"userid", "password"
*/
import javax.swing.JOptionPane;
import java.sql.*;
public class JdbcDemo
{
public static void main(String args[])
{
JOptionPane.showMessageDialog(null,"Welcome to JDBC
Demo");
int choice = -1;
String userid="scott";
String password = "tiger";
do
{
choice = getChoice();
if (choice != 0)
{
getSelected(choice, userid, password);
}
}
while ( choice != 0);
System.exit(0);
}
// jdbc:subprotocol:subname
Connection con;
String createString;
createString = "create table COFFEES " +
"(COF_NAME
varchar(32), " +
"SUP_ID int, " +
"PRICE float, " +
"SALES int, " +
"TOTAL int)";
Statement stmt;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Class.forName("myDriver.ClassName"); ?
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"userid", "password");
stmt = con.createStatement();
stmt.executeUpdate(createString);
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " +
ex.getMessage());
}
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"userid", "password");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println("Coffee Break Coffees
and Prices:");
while (rs.next()) {
String s =
rs.getString("COF_NAME"); // OR rs.getString(1);
float f = rs.getFloat("PRICE");
// OR rs.getFloat(3);
System.out.println(s + " " +
f);
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " +
ex.getMessage());
}
}
Statement stmt;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"userid", "password");
stmt = con.createStatement();
stmt.executeUpdate(createString);
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " +
ex.getMessage());
}
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"userid", "password");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " +
ex.getMessage());
}
}
Statement stmt;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"userid", "password");
stmt = con.createStatement();
stmt.executeUpdate(updateString);
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String s =
rs.getString("COF_NAME"); //1
int n =
rs.getInt("SALES"); //2
System.out.println(n + "
pounds of " + s +
"
sold this week.");
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " +
ex.getMessage());
}
}
Statement stmt=null;
String query = "SELECT COF_NAME, SALES FROM
COFFEES ";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"userid", "password");
PreparedStatement updateSales =
con.prepareStatement(
"UPDATE COFFEES SET SALES = ?
WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75);
updateSales.setString(2, "Colombian");
updateSales.executeUpdate();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String s =
rs.getString("COF_NAME"); //1
int n =
rs.getInt("SALES"); //2
System.out.println(s + "
" + n);
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " +
ex.getMessage());
}
}
Statement stmt=null;
String query = "SELECT COF_NAME, SALES FROM
COFFEES ";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"userid", "password");
PreparedStatement updateSales;
String updateString = "update COFFEES " +
"set SALES = ? where
COF_NAME like ?";
updateSales =
con.prepareStatement(updateString);
int [] salesForWeek = {175, 150, 60, 155,
90};
String [] coffees = {"Colombian",
"French_Roast", "Espresso",
"Colombian_Decaf",
"French_Roast_Decaf"};
int len = coffees.length;
for(int i = 0; i < len; i++) {
updateSales.setInt(1,
salesForWeek[i]);
updateSales.setString(2,
coffees[i]);
updateSales.executeUpdate();
// int n= updateSales.executeUpdate() to
find out how may rows have been updated
}
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String s =
rs.getString("COF_NAME"); //1
int n =
rs.getInt("SALES"); //2
System.out.println(s + "
" + n);
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " +
ex.getMessage());
}
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection (url,
"userid", "password");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println("Supplier, Coffee:");
while (rs.next()) {
String supName = rs.getString(1);
String cofName = rs.getString(2);
System.out.println(" " +
supName + ", " + cofName);
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"userid", "password");
updateSales =
con.prepareStatement(updateString);
updateTotal =
con.prepareStatement(updateStatement);
int [] salesForWeek = {175, 150, 60, 155,
90};
String [] coffees = {"Colombian",
"French_Roast",
"Es
presso", "Colombian_Decaf",
"Fr
ench_Roast_Decaf"};
int len = coffees.length;
con.setAutoCommit(false);
for (int i = 0; i < len; i++) {
updateSales.setInt(1,
salesForWeek[i]);
updateSales.setString(2,
coffees[i]);
updateSales.executeUpdate();
updateTotal.setInt(1,
salesForWeek[i]);
updateTotal.setString(2,
coffees[i]);
updateTotal.executeUpdate();
con.commit();
}
con.setAutoCommit(true);
updateSales.close();
updateTotal.close();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String c =
rs.getString("COF_NAME");
int s = rs.getInt("SALES");
int t = rs.getInt("TOTAL");
System.out.println(c + " " +
s + " " + t);
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " +
ex.getMessage());
if (con != null) {
try {
System.err.print("SQLException: ");
System.err.println(excep.getMessage());
}
}
}
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException
e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con =
DriverManager.getConnection(url,
"DPST_TRNG", "DPST_TRNG4321");
stmt = con.createStatement();
stmt.executeUpdate(createProcedure);
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException:
" + ex.getMessage());
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException
e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con =
DriverManager.getConnection(url,
"DPST_TRNG", "DPST_TRNG4321");
CallableStatement cs =
con.prepareCall("{call SHOW_SUPPLIERS}");
ResultSet rs = cs.executeQuery();
while (rs.next()) {
String c =
rs.getString("SUP_NAME");
String s =
rs.getString("COF_NAME");
System.out.println(c + "
" + s );
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException:
" + ex.getMessage());
}
Connection con;
Statement stmt;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"us
erid", "password");
con.setAutoCommit(false);
stmt = con.createStatement();
uprs.close();
stmt.close();
con.close();
} catch(BatchUpdateException b) {
System.err.println("-----
BatchUpdateException-----");
System.err.println("SQLState: " +
b.getSQLState());
System.err.println("Message: " +
b.getMessage());
System.err.println("Vendor: " +
b.getErrorCode());
System.err.print("Update counts: ");
int [] updateCounts =
b.getUpdateCounts();
for (int i = 0; i < updateCounts.length;
i++) {
System.err.print(updateCounts[i]
+ " ");
}
System.err.println("");
} catch(SQLException ex) {
System.err.println("-----
SQLException-----");
System.err.println("SQLState: " +
ex.getSQLState());
System.err.println("Message: " +
ex.getMessage());
System.err.println("Vendor: " +
ex.getErrorCode());
}
}
}//End of class
Connect to a database via JDBC-ODBC
You have to keep in mind that the bridge JDBC-ODBC is only useful in an Application, you can't use it with
JAVA Applet because ODBC requires some DLL on the client machine (forbidden for security reason).
import java.net.URL;
import java.sql.*;
class JDBCapp {
ResultSet rs;
Statement stmt;
String sql;
while (rs.next()) {
System.out.println(rs.getString("objet"));
}
rs.close();
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (theConn != null) theConn.close();
}
catch (Exception e) {
}
}
}
}
class MyConnection {
public static Connection getConnection() throws Exception {
Driver d = (Driver)Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/tech97.mdb"
);
return c;
/*
To use an already defined ODBC Datasource :
*/
}
}