Vous êtes sur la page 1sur 8

JDBC

MySQL
Pramudya Yanuanto
0856-7777943 / 021-78831053
Registering the Driver With the DriverManager

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
public class LoadDriver {

public static void main(String[] args) {


try {
// The newInstance() call
//is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception ex) {
// handle the error
}
}

Copyright Pramudya Yanuanto – pramudyo@centrin.net.id (0856-7777943)


Obtaining a Connection From the
DriverManager

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
... try {
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/test?
user=monty&password=greatsqldb");
// Do something with the Connection
.... } catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}

Copyright Pramudya Yanuanto – pramudyo@centrin.net.id (0856-7777943)


Using java.sql.Statement to
Execute a SELECT Query
// assume conn is an already created JDBC connection
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT foo FROM bar");
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT foo FROM bar")) {
rs = stmt.getResultSet(); } // Now do something with the
ResultSet
.... }
finally {
// it is a good idea to release resources in a finally{}
//block in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { // ignore }
rs = null; }
if (stmt != null) {
try { stmt.close();
} catch (SQLException sqlEx) { // ignore }
stmt = null;
} }
Copyright Pramudya Yanuanto – pramudyo@centrin.net.id (0856-7777943)
Stored Procedure Example

CREATE PROCEDURE demoSp(


IN inputParam VARCHAR(255),
INOUT inOutParam INT)

BEGIN
DECLARE z INT;
SET z = inOutParam + 1;
SET inOutParam = z;
SELECT inputParam;
SELECT CONCAT('zyxw', inputParam);
END

Copyright Pramudya Yanuanto – pramudyo@centrin.net.id (0856-7777943)


Use the Store Procedure

import java.sql.CallableStatement;
...
// Prepare a call to the stored procedure
'demoSp'
// with two parameters
// Notice the use of JDBC-escape syntax ({call
...})
CallableStatement cStmt =
conn.prepareCall("{call demoSp(?, ?)}");
cStmt.setString(1, "abcdefg");

Copyright Pramudya Yanuanto – pramudyo@centrin.net.id (0856-7777943)


Registering Output Parameters

import java.sql.Types;
// Connector/J supports both named and indexed output parameters.
//You can register output parameters using either method, as well
// as retrieve output parameters using either method, regardless of
//what method was used to register them.
//The following examples show how to use the various methods of
//registering output parameters (you should of course use only one
//registration per parameter). Registers the second parameter as
//output. Registers the second parameter as output, and uses the type
//'INTEGER' for values returned from getObject()
cStmt.registerOutParameter(2);
cStmt.registerOutParameter(2, Types.INTEGER);
// Registers the named parameter 'inOutParam'
cStmt.registerOutParameter("inOutParam");
// Registers the named parameter 'inOutParam', and uses the type
//'INTEGER' for values returned from getObject()
cStmt.registerOutParameter("inOutParam", Types.INTEGER);
...
Copyright Pramudya Yanuanto – pramudyo@centrin.net.id (0856-7777943)
Copyright Pramudya Yanuanto – pramudyo@centrin.net.id (0856-7777943)

Vous aimerez peut-être aussi