Home > Java > javaTutorial > body text

How to call mysql data in java

coldplay.xixi
Release: 2020-09-04 13:23:48
Original
8335 people have browsed it

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
Copy after login

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 + "人");
Copy after login

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,'!');
Copy after login

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);
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!