import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.charset.Charset;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.sql.ResultSet;
import java.sql.SQLException;
import myutil.DBHelper;
public class SoftWare
{
static int p=0;
MYtable jt=new MYtable();
DBHelper db1=null;
String sql;
JButton b1=new JButton("添加一行");
JButton b2=new JButton("添加所有");
JFrame f=new JFrame("读取软件信息");
JPanel pa;
SoftWare(){
f.setLocation(300,200);
f.setSize(800,500);
f.setLayout(new BorderLayout());
JScrollPane js=new JScrollPane(jt.setTable());
f.add(js,BorderLayout.CENTER);
pa=new JPanel();
GridLayout g=new GridLayout(1,2);
g.setHgap(5);
pa.setLayout(g);
pa.add(b1);
pa.add(b2);
f.add(pa, BorderLayout.SOUTH);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
try{
check();
}
catch(Exception e){
e.printStackTrace();
}
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x=jt.getTable().getSelectedRow();
String s[]=new String[4];
for(int i=0;i<4;i++)
{
s[i]=jt.getTable().getValueAt(x,i).toString();
}
db1 = new DBHelper();//创建DBHelper对象
db1.setURL("duanwu");
String sql="insert t3(name,version,company,unloadPath) values('"+s[0]+"','"+s[1]+"','"+s[2] +"','"+s[3]+"')";
db1.setSQL(sql);
db1.DBRun();
int a=0;
try {
a=db1.pst.executeUpdate();
if(a!=0)System.out.println("添加成功");
else System.out.println("添加失败");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
db1.close();//关闭连接
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
db1=new DBHelper();
db1.setURL("duanwu");
Object da[][]=jt.getData();
int x=0;
for(int a=0;a<p;a++){
sql="insert t3(name,version,company,unloadPath) values('"+da[a][0]+"','"+da[a][1]+"','"+da[a][2] +"','"+da[a][3]+"')";
db1.setSQL(sql);
db1.DBRun();
try{
x=db1.pst.executeUpdate();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(x!=0)System.out.println("添加成功");
else System.out.println("添加失败");
db1.close();//关闭连接
}
});
}
public void check() throws Exception{
Runtime rt=Runtime.getRuntime();
Process pc=rt.exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
InputStream is=pc.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is,"GBK"));
String in=null;
while((in=br.readLine())!=null){
String message[]=valuesQ(in);
if(message!=null)jt.addRow(message);
f.repaint();
}
is.close();
br.close();
pc.destroy();
}
public String[] valuesQ(String s) throws IOException{
String name;
String version;
String company;
String unloadPath;
Process ps=null;
BufferedReader br=null;
Runtime rt=Runtime.getRuntime();
ps=rt.exec("cmd /c reg query " + s + " /v DisplayName");
br=new BufferedReader(new InputStreamReader(ps.getInputStream(),"GBK"));
br.readLine(); br.readLine();
if((name=br.readLine())!=null)name=name.replaceAll("DisplayName REG_SZ ", "");
ps=rt.exec("cmd /c reg query " + s + " /v DisplayVersion");
br=new BufferedReader(new InputStreamReader(ps.getInputStream(),"GBK"));
br.readLine(); br.readLine();
if((version=br.readLine())!=null)version=version.replaceAll("DisplayVersion REG_SZ ", "");
ps = rt.exec("cmd /c reg query " + s + " /v Publisher");
br = new BufferedReader(new InputStreamReader(ps.getInputStream(),"GBK"));
br.readLine();br.readLine();
if((company=br.readLine())!=null)company =company.replaceAll("Publisher REG_SZ ", ""); //去掉无用信息
ps=rt.exec("cmd /c reg query " + s + " /v UninstallString");
br=new BufferedReader(new InputStreamReader(ps.getInputStream(),"GBK"));
br.readLine(); br.readLine();
//UninstallString REG_EXPAND_SZ or UninstallString REG_SZ
if((unloadPath=br.readLine())!=null)unloadPath=unloadPath.replaceAll("UninstallString REG_EXPAND_SZ ", "");
String re[]=new String[4];
re[0]=name;
re[1]=version;
re[2]=company;
re[3]=unloadPath;
if(re[0]==null)re=null;
return re;
}
public static void main(String[] args)
{
new SoftWare();
}
class MYtable
{
JTable table;
private Object[][] data=new Object[100][4];
private Object[] columns={"软件名称","版本号","出版商","卸载地址"};
public Object[][] getData()
{
return data;
}
public void addRow(Object da[]){
this.data[p]=da;
p++;
}
public JTable setTable(){
table=new JTable(data,columns);
return table;
}
public JTable getTable(){
return table;
}
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.charset.Charset;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.sql.ResultSet;
import java.sql.SQLException;
import myutil.DBHelper;
public class SoftWare
{
static int p=0;
MYtable jt=new MYtable();
DBHelper db1=null;
String sql;
JButton b1=new JButton("添加一行");
JButton b2=new JButton("添加所有");
JFrame f=new JFrame("读取软件信息");
JPanel pa;
SoftWare(){
f.setLocation(300,200);
f.setSize(800,500);
f.setLayout(new BorderLayout());
JScrollPane js=new JScrollPane(jt.setTable());
f.add(js,BorderLayout.CENTER);
pa=new JPanel();
GridLayout g=new GridLayout(1,2);
g.setHgap(5);
pa.setLayout(g);
pa.add(b1);
pa.add(b2);
f.add(pa, BorderLayout.SOUTH);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
try{
check();
}
catch(Exception e){
e.printStackTrace();
}
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x=jt.getTable().getSelectedRow();
String s[]=new String[4];
for(int i=0;i<4;i++)
{
s[i]=jt.getTable().getValueAt(x,i).toString();
}
db1 = new DBHelper();//创建DBHelper对象
db1.setURL("duanwu");
String sql="insert t3(name,version,company,unloadPath) values('"+s[0]+"','"+s[1]+"','"+s[2] +"','"+s[3]+"')";
db1.setSQL(sql);
db1.DBRun();
int a=0;
try {
a=db1.pst.executeUpdate();
if(a!=0)System.out.println("添加成功");
else System.out.println("添加失败");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
db1.close();//关闭连接
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
db1=new DBHelper();
db1.setURL("duanwu");
Object da[][]=jt.getData();
int x=0;
for(int a=0;a<p;a++){
sql="insert t3(name,version,company,unloadPath) values('"+da[a][0]+"','"+da[a][1]+"','"+da[a][2] +"','"+da[a][3]+"')";
db1.setSQL(sql);
db1.DBRun();
try{
x=db1.pst.executeUpdate();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(x!=0)System.out.println("添加成功");
else System.out.println("添加失败");
db1.close();//关闭连接
}
});
}
public void check() throws Exception{
Runtime rt=Runtime.getRuntime();
Process pc=rt.exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
InputStream is=pc.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is,"GBK"));
String in=null;
while((in=br.readLine())!=null){
String message[]=valuesQ(in);
if(message!=null)jt.addRow(message);
f.repaint();
}
is.close();
br.close();
pc.destroy();
}
public String[] valuesQ(String s) throws IOException{
String name;
String version;
String company;
String unloadPath;
Process ps=null;
BufferedReader br=null;
Runtime rt=Runtime.getRuntime();
ps=rt.exec("cmd /c reg query " + s + " /v DisplayName");
br=new BufferedReader(new InputStreamReader(ps.getInputStream(),"GBK"));
br.readLine(); br.readLine();
if((name=br.readLine())!=null)name=name.replaceAll("DisplayName REG_SZ ", "");
ps=rt.exec("cmd /c reg query " + s + " /v DisplayVersion");
br=new BufferedReader(new InputStreamReader(ps.getInputStream(),"GBK"));
br.readLine(); br.readLine();
if((version=br.readLine())!=null)version=version.replaceAll("DisplayVersion REG_SZ ", "");
ps = rt.exec("cmd /c reg query " + s + " /v Publisher");
br = new BufferedReader(new InputStreamReader(ps.getInputStream(),"GBK"));
br.readLine();br.readLine();
if((company=br.readLine())!=null)company =company.replaceAll("Publisher REG_SZ ", ""); //去掉无用信息
ps=rt.exec("cmd /c reg query " + s + " /v UninstallString");
br=new BufferedReader(new InputStreamReader(ps.getInputStream(),"GBK"));
br.readLine(); br.readLine();
//UninstallString REG_EXPAND_SZ or UninstallString REG_SZ
if((unloadPath=br.readLine())!=null)unloadPath=unloadPath.replaceAll("UninstallString REG_EXPAND_SZ ", "");
String re[]=new String[4];
re[0]=name;
re[1]=version;
re[2]=company;
re[3]=unloadPath;
if(re[0]==null)re=null;
return re;
}
public static void main(String[] args)
{
new SoftWare();
}
class MYtable
{
JTable table;
private Object[][] data=new Object[100][4];
private Object[] columns={"软件名称","版本号","出版商","卸载地址"};
public Object[][] getData()
{
return data;
}
public void addRow(Object da[]){
this.data[p]=da;
p++;
}
public JTable setTable(){
table=new JTable(data,columns);
return table;
}
public JTable getTable(){
return table;
}
}
}