精华内容
下载资源
问答
  • 本文yjbys小编为大家主要介绍了Java调用MySQL存储过程并获得返回值的方法,实例分析了java实现MySQL存储过程的相关技巧,具有一定参考借鉴价值,需要的'朋友可以参考下本文实例讲述了Java调用MySQL存储过程并获得返回值...

    本文yjbys小编为大家主要介绍了Java调用MySQL存储过程并获得返回值的方法,实例分析了java实现MySQL存储过程的相关技巧,具有一定参考借鉴价值,需要的'朋友可以参考下

    本文实例讲述了Java调用MySQL存储过程并获得返回值的方法。分享给大家供大家参考。具体如下:

    private void empsInDept(Connection myConnect, int deptId) throws SQLException {

    CallableStatement cStmt = myConnect.prepareCall("{CALL sp_emps_in_dept(?)}");

    cStmt.setInt(1, deptId);

    cStmt.execute();

    ResultSet rs1 = cStmt.getResultSet();

    while (rs1.next()) {

    System.out.println(rs1.getString("department_name") + " " + rs1.getString("location"));

    }

    rs1.close();

    /* process second result set */

    if (cStmt.getMoreResults()) {

    ResultSet rs2 = cStmt.getResultSet();

    while (rs2.next()) {

    System.out.println(rs2.getInt(1) + " " + rs2.getString(2) + " " + rs2.getString(3));

    }

    rs2.close();

    }

    cStmt.close();

    }

    本文实例讲述了java调用mysql存储过程的方法。分享给大家供大家参考。具体如下:

    数据库的测试代码如下 :

    1、新建表test

    create table test(

    field1 int not null

    )

    TYPE=MyISAM ;

    insert into test(field1) values(1);

    2、删除已存在的存储过程:

    -- 删除储存过程

    delimiter // -- 定义结束符号

    drop procedure p_test;

    3、mysql存储过程定义:

    create procedure p_test()

    begin

    declare temp int;

    set temp = 0;

    update test set field1 = values(temp);

    end

    4、调用方法:

    CallableStatement cStmt = conn.prepareCall("{call p_test()}");

    cStmt.executeUpdate();

    import java.sql.*;

    /**

    iGoder

    */

    public class ProcedureTest {

    /*

    表和存储过程定义如下:

    delimiter //

    DROP TABLE if exists test //

    CREATE TABLE test(

    id int(11) NULL

    ) //

    drop procedure if existssp1 //

    create procedure sp1(in p int)

    comment 'insert into a int value'

    begin

    declare v1 int;

    set v1 = p;

    insert into test(id) values(v1);

    end

    //

    drop procedure if exists sp2 //

    create procedure sp2(out p int)

    begin

    select max(id) into p from test;

    end

    //

    drop procedure if exists sp6 //

    create procedure sp6()

    begin

    select * from test;

    end//

    */

    public static void main(String[] args) {

    //callIn(111);

    //callOut();

    callResult();

    }

    /**

    * 调用带有输入参数的存储过程

    * @param in stored procedure input parametervalue

    */

    public static void callIn(int in){

    //获取连接

    Connection conn = ConnectDb.getConnection();

    CallableStatement cs = null;

    try {

    //可以直接传入参数

    //cs = conn.prepareCall("{call sp1(1)}");

    //也可以用问号代替

    cs = conn.prepareCall("{call sp1(?)}");

    //设置第一个输入参数的值为110

    cs.setInt(1, in);

    cs.execute();

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    if(cs != null){

    cs.close();

    }

    if(conn != null){

    conn.close();

    }

    } catch (Exception ex) {

    ex.printStackTrace();

    }

    }

    }

    /**

    * 调用带有输出参数的存储过程

    *

    */

    public static void callOut() {

    Connection conn = ConnectDb.getConnection();

    CallableStatement cs = null;

    try {

    cs = conn.prepareCall("{call sp2(?)}");

    //第一个参数的类型为Int

    cs.registerOutParameter(1, Types.INTEGER);

    cs.execute();

    //得到第一个值

    int i = cs.getInt(1);

    System.out.println(i);

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    if(cs != null){

    cs.close();

    }

    if(conn != null){

    conn.close();

    }

    } catch (Exception ex) {

    ex.printStackTrace();

    }

    }

    }

    /**

    * 调用输出结果集的存储过程

    */

    public static void callResult(){

    Connection conn = ConnectDb.getConnection();

    CallableStatement cs = null;

    ResultSet rs = null;

    try {

    cs = conn.prepareCall("{call sp6()}");

    rs = cs.executeQuery();

    //循环输出结果

    while(rs.next()){

    System.out.println(rs.getString(1));

    }

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    if(rs != null){

    rs.close();

    }

    if(cs != null){

    cs.close();

    }

    if(conn != null){

    conn.close();

    }

    } catch (Exception ex) {

    ex.printStackTrace();

    }

    }

    }

    }

    /**

    *获取数据库连接的类

    */

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;

    class ConnectDb {

    public static Connection getConnection(){

    Connection conn = null;

    PreparedStatement preparedstatement = null;

    try {

    Class.forName("org.gjt.mm.mysql.Driver").newInstance();

    String dbname = "test";

    String url="jdbc:mysql://localhost/"+dbname+"?user=root&password=root&useUnicode=true&characterEncoding=8859_1";

    conn= DriverManager.getConnection(url);

    } catch (Exception e) {

    e.printStackTrace();

    }

    return conn;

    }

    }

    希望本文所述对大家的java程序设计有所帮助。

    【Java调用MySQL存储过程并获得返回值的方法】相关文章:

    展开全文
  • 本文实例讲述了java调用mysql存储过程的方法。分享给大家供大家参考。具体如下:数据库的测试代码如下 :1、新建表testcreate table test(field1 int not null)TYPE=MyISAM ;insert into test(field1) values(1);2、...

    本文实例讲述了java调用mysql存储过程的方法。分享给大家供大家参考。具体如下:

    数据库的测试代码如下 :

    1、新建表test

    create table test(

    field1 int not null

    )

    TYPE=MyISAM ;

    insert into test(field1) values(1);

    2、删除已存在的存储过程:

    -- 删除储存过程

    delimiter // -- 定义结束符号

    drop procedure p_test;

    3、mysql存储过程定义:

    create procedure p_test()

    begin

    declare temp int;

    set temp = 0;

    update test set field1 = values(temp);

    end

    4、调用方法:

    CallableStatement cStmt = conn.prepareCall("{call p_test()}");

    cStmt.executeUpdate();

    import java.sql.*;

    /**

    iGoder

    */

    public class ProcedureTest {

    /*

    表和存储过程定义如下:

    delimiter //

    DROP TABLE if exists test //

    CREATE TABLE test(

    id int(11) NULL

    ) //

    drop procedure if existssp1 //

    create procedure sp1(in p int)

    comment 'insert into a int value'

    begin

    declare v1 int;

    set v1 = p;

    insert into test(id) values(v1);

    end

    //

    drop procedure if exists sp2 //

    create procedure sp2(out p int)

    begin

    select max(id) into p from test;

    end

    //

    drop procedure if exists sp6 //

    create procedure sp6()

    begin

    select * from test;

    end//

    */

    public static void main(String[] args) {

    //callIn(111);

    //callOut();

    callResult();

    }

    /**

    * 调用带有输入参数的存储过程

    * @param in stored procedure input parametervalue

    */

    public static void callIn(int in){

    //获取连接

    Connection conn = ConnectDb.getConnection();

    CallableStatement cs = null;

    try {

    //可以直接传入参数

    //cs = conn.prepareCall("{call sp1(1)}");

    //也可以用问号代替

    cs = conn.prepareCall("{call sp1(?)}");

    //设置第一个输入参数的值为110

    cs.setInt(1, in);

    cs.execute();

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    if(cs != null){

    cs.close();

    }

    if(conn != null){

    conn.close();

    }

    } catch (Exception ex) {

    ex.printStackTrace();

    }

    }

    }

    /**

    * 调用带有输出参数的存储过程

    *

    */

    public static void callOut() {

    Connection conn = ConnectDb.getConnection();

    CallableStatement cs = null;

    try {

    cs = conn.prepareCall("{call sp2(?)}");

    //第一个参数的类型为Int

    cs.registerOutParameter(1, Types.INTEGER);

    cs.execute();

    //得到第一个值

    int i = cs.getInt(1);

    System.out.println(i);

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    if(cs != null){

    cs.close();

    }

    if(conn != null){

    conn.close();

    }

    } catch (Exception ex) {

    ex.printStackTrace();

    }

    }

    }

    /**

    * 调用输出结果集的存储过程

    */

    public static void callResult(){

    Connection conn = ConnectDb.getConnection();

    CallableStatement cs = null;

    ResultSet rs = null;

    try {

    cs = conn.prepareCall("{call sp6()}");

    rs = cs.executeQuery();

    //循环输出结果

    while(rs.next()){

    System.out.println(rs.getString(1));

    }

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    if(rs != null){

    rs.close();

    }

    if(cs != null){

    cs.close();

    }

    if(conn != null){

    conn.close();

    }

    } catch (Exception ex) {

    ex.printStackTrace();

    }

    }

    }

    }

    /**

    *获取数据库连接的类

    */

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;

    class ConnectDb {

    public static Connection getConnection(){

    Connection conn = null;

    PreparedStatement preparedstatement = null;

    try {

    Class.forName("org.gjt.mm.mysql.Driver").newInstance();

    String dbname = "test";

    String url="jdbc:mysql://localhost/"+dbname+"?user=root&password=root&useUnicode=true&characterEncoding=8859_1";

    conn= DriverManager.getConnection(url);

    } catch (Exception e) {

    e.printStackTrace();

    }

    return conn;

    }

    }

    希望本文所述对大家的java程序设计有所帮助。

    展开全文
  • java调用mysql存储过程没有成功存储过程如下.DELIMITER $$DROP PROCEDURE IF EXISTS `tennis`.`sp_k`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_k`(in i_f1 integer)begininsert into tz values(...

    java调用mysql存储过程没有成功

    存储过程如下.

    DELIMITER   $$

    DROP   PROCEDURE   IF   EXISTS   `tennis`.`sp_k`$$

    CREATE   DEFINER=`root`@`localhost`   PROCEDURE   `sp_k`(in   i_f1   integer)

    begin

    insert   into   tz   values(i_f1);

    end$$

    DELIMITER   ;

    java代码如下.

    Class.forName( "com.mysql.jdbc.Driver ").newInstance();

    String   conUrl= "jdbc:mysql://localhost:3306/tennis ";

    Connection   con=DriverManager.getConnection(conUrl, "root ", "admin ");

    CallableStatement   cstmt=con.prepareCall( "{call   sp_k(?)} ");

    cstmt.setInt( "i_f1 ",333);

    boolean   hadResult=cstmt.execute();

    if(hadResult){

    System.out.println( "aa ");

    }

    cstmt.close();

    con.close();

    程序没有出现什么异常错误..不过数据库里又没有添加到数据...不知道是什么原因.

    ------解决方案--------------------

    当前的用户是否具有存储过程的CREATE 权限?

    e7ce419cf2d6ad34d01da2ceb8829eed.png

    微信 赏一包辣条吧~

    023a57327877fb4402bcc76911ec18ea.png

    支付宝 赏一听可乐吧~

    展开全文
  • JAVA调用MYSQL存储过程工程视图:代码清单:myconn.javaview plaincopy to clipboardprint?package org.apache.sh_mysql.test;import java.sql.*;public class MyConn {private static final String DRIVER = ...

    JAVA调用MYSQL存储过程

    工程视图:

    代码清单:

    myconn.java

    view plaincopy to clipboardprint?

    package org.apache.sh_mysql.test;

    import java.sql.*;

    public class MyConn {

    private static final String DRIVER = "com.mysql.jdbc.Driver";

    private static final String URL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";

    private static final String USER = "root";

    private static final String PASSWORD ="";

    static {

    try {

    Class.forName(DRIVER);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    /**

    * 获取连接

    *

    * @return

    * @throws Exception

    */

    public Connection getConnection() throws Exception {

    return DriverManager.getConnection(URL, USER, PASSWORD);

    }

    /**

    * 释放资源

    *

    * @param rs

    * @param stmt

    * @param conn

    */

    public void close(ResultSet rs, CallableStatement stmt, Connection conn) {

    try{

    if (rs != null) {

    rs.close();

    }

    if (stmt != null) {

    stmt.close();

    }

    if (conn != null) {

    conn.close();

    }

    }catch(Exception e){

    e.printStackTrace();

    }

    }

    }

    package org.apache.sh_mysql.test;

    import java.sql.*;

    public class MyConn {

    private static final String DRIVER = "com.mysql.jdbc.Driver";

    private static final String URL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";

    private static final String USER = "root";

    private static final String PASSWORD ="";

    static {

    try {

    Class.forName(DRIVER);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    /**

    * 获取连接

    *

    * @return

    * @throws Exception

    */

    public Connection getConnection() throws Exception {

    return DriverManager.getConnection(URL, USER, PASSWORD);

    }

    /**

    * 释放资源

    *

    * @param rs

    * @param stmt

    * @param conn

    */

    public void close(ResultSet rs, CallableStatement stmt, Connection conn) {

    try{

    if (rs != null) {

    rs.close();

    }

    if (stmt != null) {

    stmt.close();

    }

    if (conn != null) {

    conn.close();

    }

    }catch(Exception e){

    e.printStackTrace();

    }

    }

    }

    代码清单:

    mytest.java

    view plaincopy to clipboardprint?

    package org.apache.sh_mysql.test;

    import java.sql.*;

    public class MyTest {

    MyConn c = new MyConn();

    //带单个返回值存储过程调用

    public void handleSoleData() {

    try {

    Connection conn = c.getConnection();

    CallableStatement call = conn

    .prepareCall("{call pro_stu_count(?)}");

    call.registerOutParameter(1, Types.INTEGER);

    call.execute();

    int count = call.getInt(1);

    System.out.println(count);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    //带多个返回值存储过程调用

    public void handleBothData() {

    try {

    Connection conn=c.getConnection();

    CallableStatement call=conn.prepareCall("call pro_vi()");

    call.execute();

    ResultSet rst=call.getResultSet();

    while(rst.next())

    {

    System.out.println(rst.getInt(1)+"\t"+rst.getString(2)+"\t"+rst.getInt(3)+"\t"+rst.getString(4)+"\t"+rst.getDate(5)+"\t"+rst.getString(6));

    }

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    public static void main(String[] args) {

    MyTest t = new MyTest();

    //        t.handleSoleData();

    //        t.handleBothData();

    }

    }

    分享到:

    18e900b8666ce6f233d25ec02f95ee59.png

    72dd548719f0ace4d5f9bca64e1d7715.png

    2010-06-21 23:58

    浏览 1640

    评论

    展开全文
  • Java调用MySQL 存储过程的方法Java#存储过程2014-04-09 21:35直接上代码。private void empsInDept(Connection myConnect, int deptId) throws SQLException {CallableStatement cStmt = myConnect.prepareCall("{...
  • JAVA调用MYSQL存储过程工程视图:代码清单:myconn.javapackage org.apache.sh_mysql.test;import java.sql.*;public class MyConn {private static final String DRIVER = "com.mysql.jdbc.Driver";private static ...
  • 主要介绍了java调用mysql存储过程的方法,以实例形式较为详细的分析了mysql数据库的建立和存储过程的实现方法,需要的朋友可以参考下
  • java调用mysql存储

    2014-05-20 22:19:18
    --命令行创建存储 DELIMITER // create procedure test() begin select * from student4;...--命令行调用存储 call test(); import java.sql.Connection; import java.sql.DriverManager; import j...
  • JAVA调用MYSQL存储过程工程视图:代码清单:myconn.javapackage org.apache.sh_mysql.test;import java.sql.*;public class MyConn {private static final String DRIVER = "com.mysql.jdbc.Driver";private static ...
  • 主要介绍了Java调用MySQL存储过程并获得返回值的方法,实例分析了java实现MySQL存储过程的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
  • 本文实例讲述了Java调用MysqL存储过程并获得返回值的方法。分享给大家供大家参考。具体如下:private void empsInDept(Connection myConnect,int deptId) throws sqlException {CallableStatement cStmt = myConnect...
  • 本文实例讲述了java调用MysqL存储过程的方法。分享给大家供大家参考。具体如下:数据库的测试代码如下 :1、新建表testcreate table test(field1 int not null)TYPE=MyISAM ;insert into test(field1) values(1);2、...
  • Java 调用Mysql 存储过程

    千次阅读 2016-06-22 13:33:26
    Java 调用Mysql 存储过程
  • java 调用MySQL存储过程配置与调用一. 环境配置。1. JDK安装, MySQL的安装, 启动(省略)。注意: 在Path目录下增加MySQL的安装目录, 我的是D:/Program Files/MySQL/MySQL Server 5.0/bin2. 配置MySQL JDBC环境.下载...
  • java调用 Connection conn = MyDataSource.getConnection(); try { CallableStatement cstmt=conn.prepareCall(sql); cstmt.setString(1, "0985023223"); cstmt.registerOutParameter(2, java.sql.Types.VARCHAR); ...
  • mysql> show procedurestatus;+------+-------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--...
  • /**创建存储过程*/create procedure getsum@n int =0asdeclare @sum intdeclare @i intset @sum=0set @...=@n beginset @sum=@sum+@iset @i=@i+1endprint 'the sum is '+ltrim(rtrim(str(@sum)))/**java调用存储过程...
  • JAVA调用MYSQL存储过程

    2016-10-17 12:18:38
    Java调用MySQL存储过程   工程视图:       代码清单:   myconn.java     [c-sharp] view plain copy  print? package org.apache.sh_m
  • asp教程.net c# java调用mysql教程存储过程方法本文章主要介绍三种asp.net教程 c# java调用mysql存储过程方法,一一举例说明了关于如何创建如调用mysql存储过程的方法哦。简单存储过程create procedure `deletedb`...
  • import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;.../** 对应的存储过程DROP PROCEDURE IF EXISTS `pro_num_user`;delimiter ;;CREATE PROCEDURE `pro_nu...
  • 首先我在mysql的test数据库里定义了一个student表:create table student4(id int primary key,sanme char(5));插入几个数据做测试用:inset into student values(1,'mayi');inset into student values(2,'mayi');...
  • 展开全部java代码copy:Class.forName("com.mysql.jdbc.Driver");Connectioncon=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=111111");Stringsql="{callsel(?)}";Callab....
  • Java调用MySQL存储过程插入数据,报错Incorrect DECIMAL value: '0' for column '' at row -1; 报错信息: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized ...
  • java 调用MySQL存储过程配置与调用 一. 环境配置。 1. JDK安装, MySQL的安装, 启动(省略)。 注意: 在Path目录下增加MySQL的安装目录, 我的是 D:/Program Files/MySQL/MySQL Server 5.0/bin 2. 配置MySQL ...
  • public static final String DRIVER_CLASS = ... //驱动名public static final String URL = "jdbc:mysql://127.0.0.1:端口号/项目名?useUnicode=true&characterEncoding=UTF-8";public static fina...
  • Class.forName(...String ip = PropReader.getProperties("mysql_ip");int port = Integer.parseInt(PropReader.getProperties("mysql_port"));String serviceName = PropReader.getPropertie...
  • 本文实例讲述了java调用mysql存储过程的方法。分享给大家供大家参考。具体如下:数据库的测试代码如下 :1、新建表test create table test(field1 int not null)TYPE=MyISAM ;insert into test(field1) values(1);2、...
  • public class Test {public static final String DRIVER_CLASS = ...public static final String URL = "jdbc:mysql://127.0.0.1:3306/test";public static final String USERNAME = "root";pu...
  • Java调用MySQL存储过程需要在JDBC连接字符串加上如下内容  &noAccessToProcedureBodies=true   整个写法如下: url="jdbc:mysql://127.0.0.1:3306/ccbfl?characterEncodin

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,048
精华内容 419
关键字:

java调用mysql存储

java 订阅
mysql 订阅