Java调用MySQL的存储过程,需要用JDBC连接,环境eclipse
首先查看MySQL中的数据库的存储过程,接着编写代码调用
mysql> show procedurestatus;+------+-------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |+------+-------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+| book | findAllBook | PROCEDURE | root@localhost | 2016-09-04 11:13:31 | 2016-09-0411:13:31 | DEFINER | | gbk | gbk_chinese_ci | utf8_general_ci || book | pro_test | PROCEDURE | root@localhost | 2016-11-13 08:27:17 | 2016-11-1308:27:17 | DEFINER | | gbk | gbk_chinese_ci | utf8_general_ci || book | pro_user | PROCEDURE | root@localhost | 2016-11-13 08:44:34 | 2016-11-1308:44:34 | DEFINER | | gbk | gbk_chinese_ci | utf8_general_ci |+------+-------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+3 rows inset (0.01 sec)mysql> show createprocedurefindAllBook;+-------------+------------------------+---------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+| Procedure | sql_mode | CreateProcedure | character_set_client | collation_connection | DatabaseCollation |+-------------+------------------------+---------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+| findAllBook | NO_ENGINE_SUBSTITUTION | CREATEDEFINER=`root`@`localhost` PROCEDURE `findAllBook`()beginselect * fromtb_books;end | gbk | gbk_chinese_ci | utf8_general_ci |+-------------+------------------------+---------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+1 row inset (0.00 sec)
1.工程目录结构
2.Book.java
package com.scd.book;publicclass Book { private String name; //图书名称privatedouble price; //价格privateint bookCount; //数量private String author; //作者public String getName() { //System.out.println(name);return name; } publicvoidsetName(String name) { this.name = name; } publicdoublegetPrice() { return price; } publicvoidsetPrice(double price) { this.price = price; } publicintgetBookCount() { return bookCount; } publicvoidsetBookCount(int bookCount) { this.bookCount = bookCount; } public String getAuthor() { return author; } publicvoidsetAuthor(String author) { //System.out.println(author);this.author = author; }}
2.FindBook.java
package com.scd.book;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;publicclassFindBook {/** * 获取数据库连接 * @return Connection对象 */public Connection getConnection() { Connection conn = null; //数据库连接try { Class.forName("com.mysql.jdbc.Driver"); //加载数据库驱动,注册到驱动管理器/*数据库链接地址*/ String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8"; String username = "root"; String password = "123456"; /*创建Connection链接*/ conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e){ e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn;