作者:徐徐回忆_545 | 来源:互联网 | 2023-09-05 19:16
package com.config.update;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class InstallFileConfiguration {
private final static Logger logger = Logger.getLogger(InstallFileConfiguration.class);
JFrame f = new JFrame();
Panel ppTotal;
JLabel l1;
JLabel l2;
JLabel l3;
JTextField t1;
JTextField t2;
JTextField t3;
// prperties参数输出流
FileOutputStream fos;
FileInputStream fis;
StringBuffer filetext=new StringBuffer();
// 取得地址
String cOnfigurationFile= "h:/entityengine.xml";
JButton jButton;
static String path="";
static String rootUrl = System.getProperty("user.dir");
static String cOnfigFilePath="\\components\\entity\\config";
public static void main(String[] args) {
path=rootUrl+configFilePath+"\\entityengine.xml";
System.out.println(path);
//
path= InstallFileConfiguration.class.getClassLoader().getResource("entityengine.xml").getPath();
//
path=path.substring(1);
//
System.out.println(path);
new InstallFileConfiguration().Show(path);
}
// 显示方法
public void Show(String path) {
if("".equals(path)||path==null){
logger.error("没有找到相关配置文件信息");
return;
}
ppTotal= new Panel();
l1 = new JLabel();
l2 = new JLabel();
l3 = new JLabel();
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
jButton = new JButton();
// 创建list集合
List list = new ArrayList();
// 属性集合对象
Properties prop = new Properties();
// 设置窗体宽度50,850
f.setPreferredSize(new Dimension(500,400));
//jdom解析xml
//使用JDOM首先要指定使用什么解析器。如:
String uriValue="";
String uNameValue="";
String pwdValue="";
SAXBuilder builder=new SAXBuilder(false);// 这表示使用的是默认的解析器
try {
Document doc=builder.build(path);
Element rootElement=doc.getRootElement();//得到根节点
List dataSourceList=rootElement.getChildren("datasource");//得到元素(节点)的集合:
if(dataSourceList.size()==1){//配置文件只有一个datasource节点
List inlineList=dataSourceList.get(0).getChildren("inline-jdbc");
if(inlineList.size()==1){//配置文件只有一个inline-jdbc节点
uriValue=inlineList.get(0).getAttribute("jdbc-uri").getValue();
uNameValue=inlineList.get(0).getAttribute("jdbc-username").getValue();
pwdValue=inlineList.get(0).getAttribute("jdbc-password").getValue();
}else{
logger.error("请检查配置信息节点inline-jdbc是否正确");
}
}else{
logger.error("请检查配置信息节点datasource是否正确");
}
} catch (JDOMException | IOException e1) {
e1.printStackTrace();
logger.error("解析数据库配置文件发生错误");
}
// jdbc_uri,username,password取得需要更改的值
// 显示文件内容
l1.setText("数据库名称");//jdbc-uri
t1.setText(uriValue);
// t1,t2,t3添加标示名称
//t1.setName(jdbc_uri);
l2.setText("用 户 名");//jdbc-username
t2.setText(uNameValue);
//t2.setName(username);
l3.setText("密 码");//jdbc-password
t3.setText(pwdValue);
// 创建文件内容的panel
Panel p = new Panel();
p.add(l1);
p.add(l2);
p.add(l3);
p.add(t1);
p.add(t2);
p.add(t3);
// 设置样式
setLayout(p,l1,l2,l3,t1,t2,t3);
// String aa = textValue;
// p.add(textValue);
// 总panel中添加文件内容的panel对象
ppTotal.add(p);
ppTotal.add(jButton);
// 如果需要垂直方向滚动,则加上这句话
ScrollPane scroll = new ScrollPane();
// 设置pp1面板的长框
ppTotal.setPreferredSize(new Dimension(50,400));
scroll.add(ppTotal);
f.add(scroll);
jButton.setText("确定修改");
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Save();
}
});
// ppTotal.add(jButton,BorderLayout.NORTH);
f.pack();
f.setVisible(true);
}
// 保存方法
public void Save() {
String[] nameValues={"数据库名称","用户名","密码"};
//获取到输入的值
String newvalues[] = {t1.getText(),t2.getText(),t3.getText()};
for (int i = 0; i
System.out.println("修改后'"+nameValues[i]+"'的值为:"+newvalues[i]);
}
//jdom解析xml
//使用JDOM首先要指定使用什么解析器。如:
SAXBuilder builder=new SAXBuilder(false);// 这表示使用的是默认的解析器
Document doc=null;
try {
doc=builder.build(configurationFile);
Element rootElement=doc.getRootElement();//得到根节点
List dataSourceList=rootElement.getChildren("datasource");//得到元素(节点)的集合:
if(dataSourceList.size()==1){//配置文件只有一个datasource节点
//1 修改datasouce节点的属性schema-name的值
dataSourceList.get(0).setAttribute("schema-name", t2.getText());
//2 修改uri,username,pwd
List inlineList=dataSourceList.get(0).getChildren("inline-jdbc");
if(inlineList.size()==1){//配置文件只有一个inline-jdbc节点
inlineList.get(0).setAttribute("jdbc-uri", t1.getText());
inlineList.get(0).setAttribute("jdbc-username", t2.getText());
inlineList.get(0).setAttribute("jdbc-password", t3.getText());
}else{
logger.error("请检查配置信息节点inline-jdbc是否正确");
}
}else{
logger.error("请检查配置信息节点datasource是否正确");
}
} catch (JDOMException | IOException e) {
e.printStackTrace();
logger.error("修改数据库配置文件发生错误");
}
//保存Document的修改到XML文件中:
XMLOutputter outputter=new XMLOutputter();
try {
outputter.output(doc,new FileOutputStream(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("将修改信息写入错误");
}
}
public static void replaceTxtByStr(String oldStr, String replaceStr,
String path) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null
&& !temp.equals(oldStr); j++) {
buf = buf.append(temp);
// buf = buf.append(System.getProperty("line.separator"));
}
// 将内容插入
buf = buf.append(replaceStr);
// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
// buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 更改名称方法
public void rename(String url) {
try {
File oldFile = new File(url);
if (!oldFile.exists()) {
oldFile.createNewFile();
}
System.out.println("修改前文件名称是:" + oldFile.getName());
String rootPath = oldFile.getParent();
System.out.println("根路径是:" + rootPath);
File newFile = new File(rootPath + File.separator + "tnsnames.ora");
System.out.println("修改后文件名称是:" + newFile.getName());
if (oldFile.renameTo(newFile)) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete();
} catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
public void setLayout(Panel p, JLabel l1, JLabel l2, JLabel l3,
JTextField t1, JTextField t2, JTextField t3) {
// javax.swing.GroupLayout panel1Layout = new
// javax.swing.GroupLayout(p);
// 设置样式
javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(p);
p.setLayout(panel1Layout);
panel1Layout
.setHorizontalGroup(panel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
panel1Layout
.createSequentialGroup()
.addGap(84, 84, 84)
.addGroup(
panel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(l3)
.addComponent(l1)
.addComponent(
l2,
javax.swing.GroupLayout.Alignment.LEADING))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
43, Short.MAX_VALUE)
.addGroup(
panel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
panel1Layout
.createSequentialGroup()
.addGroup(
panel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(
t1,
javax.swing.GroupLayout.PREFERRED_SIZE,
250,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
t2,
javax.swing.GroupLayout.PREFERRED_SIZE,
250,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
t3,
javax.swing.GroupLayout.PREFERRED_SIZE,
250,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(
76,
76,
76))
.addGroup(
panel1Layout
.createSequentialGroup()
.addComponent(
t3)
.addContainerGap()))));
panel1Layout
.setVerticalGroup(panel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
panel1Layout
.createSequentialGroup()
.addGap(55, 55, 55)
.addGroup(
panel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
t1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(l1))
.addGap(25, 25, 25)
.addGroup(
panel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(l2)
.addComponent(
t2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addGroup(
panel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(l3)
.addComponent(
t3,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(126, Short.MAX_VALUE)));
}
}xml文件内容:-->