Reputation: 3578
I have an assignment to create a java Swing application to do some stuff with a mysql database, I have planed to set the database connection properties in a .properties file. In that application user should be able to change the database properties through the application. The problem I've got is how to read and write a properties file through a swing application.
try {
Properties prop = new Properties();
//reading properties
FileInputStream in = new FileInputStream("conf/properties.xml");
prop.loadFromXML(in);
System.out.println(prop.getProperty("driver"));
in.close();
//Writing properties
FileOutputStream out = new FileOutputStream("conf/properties.xml");
prop.setProperty("username", "root");
prop.storeToXML(out, "rhym");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
xml file..
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>database configuration</comment>
<entry key="driver">com.mysql.jdbc.Driver</entry>
<entry key="ip">127.0.0.1</entry>
<entry key="port">3306</entry>
<entry key="database">ofm_mnu_jvs</entry>
<entry key="username">user1</entry>
<entry key="password">123789</entry>
</properties>
Upvotes: 3
Views: 11059
Reputation: 8395
http://www.java-tips.org/java-se-tips/java.util/how-to-read-and-write-a-properties-file.html or google Load properties file in java .
Actually reading a properties file will be done is some of your method. So its not at all depends upon Swing. Just read/load the property data in any of your collections and then execute any Swing methods or Swing components to fetch the same and display.
Upvotes: 2
Reputation: 4569
Sounds like a program design exercise to me :)
First, you need to write code that can handle persisting Java's Properties
object to disk, and retrieving Properties
from disk. You can do this in many ways, but the best way is to use Java Properties syntax to save the contents of the Properties
object to a user-editable text file. Your parser will just have to be smart enough to figure out how to read text from the file back into a Properties
object, but it's really not that hard to do.
Once your program is able to correctly read/write Java Properties syntax from files, you can write your User Interface to deal only with Properties
object instances. The UI could tell your persistence objects/methods to save the Properties
instance each time the user changes a field or value.
Bottom line is, it's most important to figure out how to break up this program into smaller pieces. You could just as easily write a bunch of monolithic code that directly saves your properties files from ActionListeners in Swing, but none of that code would be reuseable. Break your code up into smaller objects (Parser object, UI object), then focus only on those smaller pieces, one at a time, until you can get them all working together to accomplish your goal.
Upvotes: 4
Reputation: 36611
You do not read/write the Properties
through a Swing application. You just read/write the Properties
as you would do it in any Java application (and which is documented in the class javadoc of the Properties
class)
Once you read the .properties file into a Properties
object, you can build a UI to customize that Properties
instance. For example when an entry represents a boolean value, you can use a checkbox in the UI, and update the Properties
instance when the user toggles the selected state of the checkbox.
Upvotes: 3