Académique Documents
Professionnel Documents
Culture Documents
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 {
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());
}
BEGIN
DECLARE z INT;
SET z = inOutParam + 1;
SET inOutParam = z;
SELECT inputParam;
SELECT CONCAT('zyxw', inputParam);
END
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");
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)