Home  >  Article  >  Java  >  How to call mysql data in java

How to call mysql data in java

coldplay.xixi
coldplay.xixiOriginal
2020-09-04 13:23:488270browse

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.

How to call mysql data in java

[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 &gt ; 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;

Call function

select hello('world');

The following is the function that Java calls Mysql

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 attention

php 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn