java 通过读配制文件的方式连接库。 信息保存在文件内

# 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: [email protected]
Reputation: 22

ID: 2
Username: bb44
Email: [email protected]
Reputation: 1

ID: 3
Username: je22
Email: [email protected]
Reputation: 49

ID: 4
Username: ae5
Email: [email protected]
Reputation: 64

ID: 5
Username: ls5
Email: [email protected]
Reputation: 10

ID: 6
Username: ay0
Email: [email protected]
Reputation: 1

ID: 7
Username: je1
Email: [email protected]
Reputation: 99

ID: 8
Username: mk57
Email: [email protected]
Reputation: 15

ID: 9
Username: tt34
Email: [email protected]
Reputation: 90

ID: 10
Username: cc100
Email: [email protected]
Reputation: 10

你可能感兴趣的:(java,开发语言)