To access the database from a Java stored function/procedure, you must use the server-side JDBC driver. As Java stored functions/procedures are executed within the database, there is no need to make the connection to the server-side JDBC driver again. To acquire a connection to the database using the server-side JDBC driver, you can either use "jdbc:default:connection:" as the URL for JDBC connection, or call the getDefaultConnection() method of the cubrid.jdbc.driver.CUBRIDDriver class.
Class.forName("cubrid.jdbc.driver.CUBRIDDriver");
Connection conn = DriverManager.getConnection("jdbc:default:connection:");
or
cubrid.jdbc.driver.CUBRIDDriver.getDefaultConnection();
If you connect to the database using the JDBC driver as shown above, the transaction in the Java stored function/procedure is ignored. That is, database operations executed in the Java stored function/procedure belong to the transaction that called the Java stored function/procedure. In the following example, conn.commit() method of the Athlete class is ignored.
import java.sql.*;
public class Athlete{
public static void Athlete(String name, String gender, String nation_code, String event) throws SQLException{
String sql="INSERT INTO ATHLETE(NAME, GENDER, NATION_CODE, EVENT)" + "VALUES (?, ?, ?, ?)";
try{
Class.forName("cubrid.jdbc.driver.CUBRIDDriver");
Connection conn = DriverManager.getConnection("jdbc:default:connection:");
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, gender);
pstmt.setString(3, nation_code);
pstmt.setString(4, event);;
pstmt.executeUpdate();
pstmt.close();
conn.commit();
conn.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}