# JDBC配置
db.url=jdbc:mysql://mysql.sqlpub.com:3306/huangjin
db.username=XXXXXXXX
db.password=fc12f7a5215e8e0a
package com.abc.project3;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;public class C {public static void main(String[] args) throws IOException, SQLException {String dbUrl = null;String dbUsername = null;String dbPassword = null;Properties properties = new Properties();FileInputStream fis = null;String fName = "config.properties";File file = new File(fName);if (!file.isFile()) {System.err.println(fName + " 文件不存在");System.exit(0);}// 读取配置文件fis = new FileInputStream(fName);properties.load(fis);// 获取 JDBC 连接信息dbUrl = properties.getProperty("db.url");dbUsername = properties.getProperty("db.username");dbPassword = properties.getProperty("db.password");if(fis!=null)fis.close();// 打印 JDBC 连接信息System.out.println("JDBC URL: " + dbUrl);System.out.println("Username: " + dbUsername);System.out.println("Password: " + dbPassword);Connection con = DriverManager.getConnection(dbUrl,dbUsername,dbPassword);//4.获取执行者对象CallableStatement stat = con.prepareCall("{CALL fun(?)}");ResultSet resultSet = null;for (int i = 1; i <= 10; i++) {stat.setInt(1, i);// 执行存储过程stat.execute();// 获取结果集resultSet = stat.getResultSet();while (resultSet.next()) {int id = resultSet.getInt("id"); // 假设id是结果集中的字段名称String username = resultSet.getString("username"); // 假设username是结果集中的字段名称String email = resultSet.getString("email"); // 假设email是结果集中的字段名称int reputation =resultSet.getInt("reputation");// 获取其他字段值// 打印或处理记录System.out.println("ID: " + id);System.out.println("Username: " + username);System.out.println("Email: " + email);System.out.println("Reputation: "+ reputation);System.out.println();}}if (resultSet != null)resultSet.close();stat.close();con.close();}
}
JDBC URL: jdbc:mysql://mysql.sqlpub.com:3306/huangjin
Username: XXXXXXXX
Password: fc12f7a5215e8e0a
ID: 1
Username: ml5
Email: michael@email.com
Reputation: 22ID: 2
Username: bb44
Email: bob@email.com
Reputation: 1ID: 3
Username: je22
Email: jane@email.com
Reputation: 49ID: 4
Username: ae5
Email: alice@email.com
Reputation: 64ID: 5
Username: ls5
Email: lisa@email.com
Reputation: 10ID: 6
Username: ay0
Email: anthony@email.com
Reputation: 1ID: 7
Username: je1
Email: joe@email.com
Reputation: 99ID: 8
Username: mk57
Email: mark@email.com
Reputation: 15ID: 9
Username: tt34
Email: tim@email.com
Reputation: 90ID: 10
Username: cc100
Email: colton@email.com
Reputation: 10