【问题标题】:How to run SQL(MYSQL) stored procedure in JAVA jdbc?如何在 JAVA jdbc 中运行 SQL(MYSQL) 存储过程?
【发布时间】:2014-06-13 23:32:05
【问题描述】:

我是 MySQL 用户,我一直在 MySQL Workbench 中使用以下语句: (这些陈述基于Select column names whose entries are not null

SET group_concat_max_len = 4294967295;

SELECT GROUP_CONCAT(
 ' SELECT ',QUOTE(COLUMN_NAME),
 ' FROM   ( select * from table_name where s3_01 = ', coloumn1,' ) abc',
 ' WHERE `',REPLACE(COLUMN_NAME, '`', '``'),'` IS NOT NULL',
 ' HAVING COUNT(*)'
SEPARATOR ' UNION ALL ')
INTO   @sql
FROM   INFORMATION_SCHEMA.COLUMNS
WHERE  TABLE_SCHEMA = DATABASE()
   AND TABLE_NAME = 'table_name';

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

虽然它在我的工作台上工作,但我不知道如何让它在 java 中工作。 例如,我做了以下代码:

String sql1 = "SET group_concat_max_len = 4294967295;";
String sql2 = " SELECT GROUP_CONCAT(' SELECT ',QUOTE(COLUMN_NAME), ' FROM   ( select * from ptc_weight where s3_01 = ',column1,' ) abc', ' WHERE `',REPLACE(COLUMN_NAME, '`', '``'),'` IS NOT NULL', ' HAVING COUNT(*)' SEPARATOR ' UNION ALL ') INTO   @sql FROM   INFORMATION_SCHEMA.COLUMNS WHERE  TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ptc_weight'; ";
String sql3 = " PREPARE stmt FROM @sql; ";
String sql4 = " EXECUTE stmt;"; 
String sql5 = " DEALLOCATE PREPARE stmt;";

String[] result = getResult(sql1+sql2+sql3+sql4+sql5);

public static String[][] getResult(String sql) {
        System.out.println(sql);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String[][] resultTable = null;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            ResultSetMetaData result = rs.getMetaData();
            int rowNum=0;
            // Go to the last row 
            rs.last(); 
            rowNum = rs.getRow(); 
        // Reset row before iterating to get data 
            rs.beforeFirst();
            int colNum = result.getColumnCount();

            resultTable = new String[rowNum][colNum];
            for(int itr1=0; itr1<rowNum; itr1++){
                rs.next();
                for(int itr2=0; itr2<colNum; itr2++){
                    resultTable[itr1][itr2] =     rs.getObject(itr2+1).toString();
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            dbclose(con, ps, rs);
        }// finally
        return resultTable;
    }

但是,它不起作用。我想我为使用存储过程编写了错误的代码,但我不知道如何处理这个问题。

【问题讨论】:

    标签: java mysql sql jdbc mysql-workbench


    【解决方案1】:
    CallableStatement callableStatement = null;
    
    String getDBUSERByUserIdSql = "{call getDBUSERByUserId(?,?,?,?)}";
    callableStatement.setInt(1, 10);
    callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR);
    callableStatement.registerOutParameter(3, java.sql.Types.VARCHAR);
    callableStatement.registerOutParameter(4, java.sql.Types.DATE);
    
    // execute getDBUSERByUserId store procedure
    callableStatement.executeUpdate();
    
    String userName = callableStatement.getString(2);
    String createdBy = callableStatement.getString(3);
    Date createdDate = callableStatement.getDate(4);
    
    System.out.println("UserName : " + userName);
    System.out.println("CreatedBy : " + createdBy);
    System.out.println("CreatedDate : " + createdDate);
    

    Here Is Full Example。您可以根据需要修改代码。

    具有较少参数和结果集的简单方法:

    CallableStatement cstmt = con.prepareCall("{call getEmployeeDetails(?, ?)}");
    cstmt.setInt("employeeId", 123);
    cstmt.setInt("companyId", 456);
    ResultSet rs = cstmt.executeQuery();
    

    【讨论】:

      猜你喜欢
      • 2013-02-07
      • 2021-06-14
      • 2012-10-24
      • 2013-12-09
      • 2018-10-31
      • 2012-07-24
      • 2016-05-03
      • 1970-01-01
      • 2011-04-28
      相关资源
      最近更新 更多