Java从配置文件读取参数连接MySQL

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.sgcc</groupId>
   <artifactId>Test</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
       <maven.compiler.source>8</maven.compiler.source>
       <maven.compiler.target>8</maven.compiler.target>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

   <dependencies>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>8.0.23</version>
       </dependency>
   </dependencies>

   <build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-shade-plugin</artifactId>
               <version>3.1.0</version>
               <executions>
                   <execution>
                       <phase>package</phase>
                       <goals>
                           <goal>shade</goal>
                       </goals>
                       <configuration>
                           <transformers>
                               <transformer
                                       implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                   <mainClass>com.sgcc.Main</mainClass>
                               </transformer>
                           </transformers>
                       </configuration>
                   </execution>
               </executions>
           </plugin>
       </plugins>
   </build>

</project>

 

2.Java 代码

package com.sgcc;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class Main {

   public static String url = null;
   public static  String user = null;
   public static  String password = null;

   public static  String sql = null;

   private  PreparedStatement ps = null;
   private  Connection conn = null;
   private  ResultSet rs = null;

   //获取Connection连接对象的方法,使用static方便之后在其他类中调用
   public  Connection getConn() {
       try {
           String name = "com.mysql.cj.jdbc.Driver";
           Class.forName(name);
           conn = DriverManager.getConnection(url, user, password);//获取连接
       } catch (SQLException | ClassNotFoundException e) {
           e.printStackTrace();
       }
       return conn;
   }

   //关闭资源的方法
   public  void close() {
       if (rs != null) {
           try {
               rs.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
       if (ps != null) {
           try {
               ps.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
       if (conn != null) {
           try {
               conn.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }

   }

   //执行查询,打印结果
   public  void  executesql(){

       Connection myconn = getConn();

       try {
           ps = myconn.prepareStatement(sql);
           rs = ps.executeQuery();
           // 获取结果集的元数据信息
           ResultSetMetaData metaData = rs.getMetaData();
           int columnCount = metaData.getColumnCount();
           // 遍历结果集
           while (rs.next()) {
               // 获取所有列的数据
               StringBuffer sbf = new StringBuffer();
               for(int i=1;i<=columnCount;i++) {
                   String str = rs.getString(i);
                   sbf.append(str + "\t");
               }
               System.out.println(sbf);
           }

       } catch (SQLException throwables) {
           throwables.printStackTrace();
       } finally {
           close();
       }

   }


   public static void main(String[] args) {

       if(args.length == 0){
           System.out.println("ERROR: parameter is null.");
           System.exit(-1);
       }
       //获取配置文件名称
       String  propFile = args[0];

       Properties prop = new Properties();

       // 加载properties文件
       try {
           prop.load(new FileInputStream(propFile));
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
       // 获取属性值
       url = prop.getProperty("database.url").trim();
       user = prop.getProperty("database.user").trim();
       password = prop.getProperty("database.password").trim();
       sql = prop.getProperty("sql").trim();

       //判断连接参数是否为空
       if(url == null || url.equals("")){
           System.out.println("ERROR: database.url is null");
           System.exit(-1);
       }
       if(user == null || user.equals("")){
           System.out.println("ERROR: database.user is null");
           System.exit(-1);
       }
       if(password == null || password.equals("")){
           System.out.println("ERROR: database.password is null");
           System.exit(-1);
       }
       if(sql == null || sql.equals("")){
           System.out.println("ERROR: sql is null");
           System.exit(-1);
       }

       Main  test = new Main();
       test.executesql();


   }
}

 

3.配置文件

创建config.properties,并设置相关信息

database.url=jdbc:mysql://localhost:3306/test?&useSSL=false&serverTimezone=UTC
database.user=root
database.password=Root@1234
sql=select * from test limit 10

 

4.运行程序

java -jar Test-1.0-SNAPSHOT.jar conf.properties

 

posted @ 2024-04-11 15:28  业余砖家  阅读(11)  评论(0编辑  收藏  举报