本文介绍了用户名,密码等硬编码到程序中 - 如何从文件中选择它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在ECLIPSE中编写了一些代码。您可以看到,用户名,密码,数据库URL,SQL查询硬编码到我的程序中。每次更改密码,我的数据库的用户名或修改查询时,我不想更改代码并重新编译。因此,我想将这些参数放在一个文件(text,XML,json ???)中。我想能够轻松地更改参数。那么我该怎么办?我应该使用什么样的文件?我想避免XML,json因为
你需要知道两者。代码越来越大,XML和JSON可能很困难。
import java.sql。*;
public class JDBCDemo {
static String query =
SELECT customerno,name+
FROM customers;;
public static void main(String [] args)
{
String username =cowboy;
String password =123456;
尝试
{
连接con = DriverManager.getConnection(jdbc:mysql:// localhost:3306 / business,用户名,密码);
语句stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
} catch(SQLException ex){System.out.println(Exception!+ ex);}
}
}
解决方案
您可以使用
并读取.properties文件中的值。
I have made some code in ECLIPSE. As you can see, the username, password, database URL, SQL Queries are hardcoded into my program. I don't want to change my code and recompile it every time I change the password, username of my database or modify the query. For that reason, I want to put these "parameters" inside a file (text, XML, json ???). I want to be able to change the parameters easily.So what should I do ? What kind of file should I use ? I want to avoid XML, json becauseyou need to know both well. XML and JSON can be difficult when the code grows big.
import java.sql.*;
public class JDBCDemo {
static String query =
"SELECT customerno, name " +
"FROM customers;";
public static void main(String[]args)
{
String username = "cowboy";
String password = "123456";
try
{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/business", username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
}catch(SQLException ex){System.out.println("Exception! " + ex);}
}
}
解决方案 You can usePropertiesand read the values from a .properties file.
这篇关于用户名,密码等硬编码到程序中 - 如何从文件中选择它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!