Java method of calling mysql data: first define a process to obtain the total number of records in the users table, and set 10 to the variable count; then modify the result character of the mysql statement; then overwrite the result to the variable a; finally Display the value of variable a.
[Related learning recommendations: php programming (video), mysql tutorial】
Java method of calling mysql data:
Process
Define a process to obtain the total number of records in the users table and set 10 to the variable count
create procedure simpleproc(out count int) begin select count(id) into count from users; end
Modify the result symbol of the mysql statement to;
mysql > ; delimiter ;
Call the procedure and return the result to variable a, @ is the symbol that defines the variable
call simpleproc(@a);
Display the value of variable a
select @a;
The following is the process of calling Mysql from Java
String sql = "{call simpleproc(?)}"; Connection conn = JdbcUtil.getConnection(); CallableStatement cstmt = conn.prepareCall(sql); cstmt.registerOutParameter(1,Types.INTEGER); cstmt.execute(); Integer count = cstmt.getInt(1); System.out.println("共有" + count + "人");
Function
Modify the result character of the mysql statement to
mysql > delimiter
Define a function to complete string splicing
create function hello( s char(20) ) returns char(50) return concat('hello,',s,'!');
Modify the result symbol of the mysql statement to;
##mysql > delimiter;
select hello('world');
String sql = "{? = call hello(?)}"; Connection conn = JdbcUtil.getConnection(); CallableStatement cstmt = conn.prepareCall(sql); cstmt.registerOutParameter(1,Types.VARCHAR); cstmt.setString(2,"zhaojun"); cstmt.execute(); String value = cstmt.getString(1); System.out.println(value); JdbcUtil.close(cstmt); JdbcUtil.close(conn);
If you want to learn more about programming, please pay attentionphp trainingcolumn!
The above is the detailed content of How to call mysql data in java. For more information, please follow other related articles on the PHP Chinese website!