MySQL PROC :
-----------------------------------------------------------------------------------------------------------------------
CREATE DEFINER=`root`@`%` PROCEDURE `proc_sequence`(OUT sequence varchar(25))
BEGIN
SELECT CONCAT(prefix, imsi, Date_format(yearmonthday,'%Y%m%d'), sufix) INTO sequence
FROM ra_sequence
WHERE seq_id=1 ;
UPDATE ra_sequence SET yearmonthday = current_date , sufix =sufix + 1
WHERE prefix='xg' AND imsi='00000000001' AND seq_id = 1 ;
END
-----------------------------------------------------------------------------------------------------------------------
JAVA CODE
==================================================================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.CallableStatement;
public class ConnectionMySQL {
public static Connection getConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/redantsDB?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "123";
Class.forName(driver);
Connection conn = (Connection) DriverManager.getConnection(url,
username, password);
return conn;
}
public static void main(String[] args) {
Connection connection = null;
CallableStatement callableStatement = null;
try {
connection = getConnection();
String proc_name = "{CALL proc_sequence(?)}";
callableStatement = (CallableStatement) connection
.prepareCall(proc_name);
callableStatement.registerOutParameter(1, java.sql.Types.VARCHAR);
callableStatement.executeUpdate();
System.out.println(callableStatement.getString(1));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != callableStatement) {
callableStatement.close();
}
if (null != connection) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}