通过IO流进行文件的输入输出;
1) 点击确定,将SQL脚本(data.sql)读取并执行,然后结果返回到(out.txt)中去,
2)点击开始比对,把(out.txt)和(true.txt)进行比较,最后返回结果到(result.txt)中;
3)点击打开目录,可以打开相对路径;
输出结果同时显示在文本框中;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package swingsql;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*06
* @author Highgo
*/
public class NewJFrame extends javax.swing.JFrame {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/samp_db?serverTimezone=GMT";
// Database credentials
static String USER = "root";
static String PASS = "zyl123";
static final String FILE_PATH = "src/out.txt";
static final String COMPARED_FILE_PATH = "src/true.txt";
static final String RESULT_FILE_PATH = "src/result.txt";
static final String DATA_SQL = "src/data.sql";
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
this.setTitle("SwingSQL测试");
setLocationRelativeTo(null);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
text1User = new java.awt.TextField();
text2Password = new java.awt.TextField();
label1User = new java.awt.Label();
label2Password = new java.awt.Label();
textArea1 = new java.awt.TextArea();
label3Start = new java.awt.Label();
jButton1User = new javax.swing.JButton();
jButton2Password = new javax.swing.JButton();
jButton3Start = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
text1User.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
text1User.setText("root");
text1User.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField1ActionPerformed(evt);
}
});
text2Password.setText("zyl123");
text2Password.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField2ActionPerformed(evt);
}
});
label1User.setText("数据库");
label2Password.setText("密码");
label3Start.setText("功能");
jButton1User.setText("打开目录");
jButton1User.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2Password.setText("开始比对");
jButton2Password.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3Start.setText("确认");
jButton3Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 52, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(label3Start, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2Password)
.addComponent(jButton1User))
.addGap(331, 331, 331))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(label2Password, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(label1User, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(text1User, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(text2Password, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
.addComponent(jButton3Start))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(47, 47, 47))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label1User, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(text1User, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(text2Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label2Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3Start))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label3Start, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1User))
.addGap(29, 29, 29)
.addComponent(jButton2Password)
.addContainerGap(68, Short.MAX_VALUE))
);
pack();
}
private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void textField2ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
textArea1.append("打开目录" + "\n");
getRootPane().setDefaultButton(jButton1User);
try {
Desktop.getDesktop().open(new File("src/"));
} catch (IOException e) {
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
textArea1.append("开始比对" + "\n");
getRootPane().setDefaultButton(jButton2Password);
long startTime = System.currentTimeMillis();
try {
BufferedReader br = new BufferedReader(new FileReader(FILE_PATH));
BufferedReader cbr = new BufferedReader(new FileReader(COMPARED_FILE_PATH));
cbr.mark(90000000);
BufferedWriter rbw = new BufferedWriter(new FileWriter(RESULT_FILE_PATH));
try {
int lineNum = 1;
String lineStr = null;
String brStr = null;
String cbrStr = null;
//while循环直到两个文件某个文件为空行
while((brStr = br.readLine()) != null &&(cbrStr = cbr.readLine()) != null)
{
if (brStr.equals(cbrStr)) {
lineStr = "<<<Equal: " + lineNum + "\n";
} else
{
lineStr = "###NOTEqual: " + lineNum + "\n";
}
lineNum++;
textArea1.append(lineStr);
rbw.write(lineStr);
}
//while循环输出多出的行数
while ((cbrStr = cbr.readLine()) != null)
{
lineStr = "---true.txt-----: " + lineNum + "\n";
lineNum++;
textArea1.append(lineStr);
rbw.write(lineStr);
}
while((brStr = br.readLine()) != null )
{
lineStr = "---out.txt------: " + lineNum + "\n";
lineNum++;
textArea1.append(lineStr);
rbw.write(lineStr);
}
} catch (Exception e) {
} finally {
br.close();
cbr.close();
rbw.close();
long endTime = System.currentTimeMillis();
textArea1.append((endTime - startTime) / 1000D + "s");
}
} catch (IOException e) {
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//回车
//当选中时把按钮设置为默认按钮
textArea1.append("确认 " + "\n");
getRootPane().setDefaultButton(jButton3Start);
long startTime = System.currentTimeMillis();
USER = text1User.getText();
PASS = text2Password.getText();
Connection conn = null;
Statement stmt = null;
try {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
}
catch(SQLException se)
{
textArea1.append("数据库名或密码错误!" + "\n");
return;
}
stmt = conn.createStatement();
BufferedReader br = new BufferedReader(new FileReader(DATA_SQL));
BufferedWriter wbw = new BufferedWriter(new FileWriter(FILE_PATH));
String str = null;
textArea1.append("连接成功 " + "\n");
try {
String sql= null;
while((sql = br.readLine()) != null )
{
textArea1.append(sql + "\n");
conn.createStatement().executeUpdate(sql);
}
String sqlselect = "select * from T_STUDENT";
conn.createStatement().executeQuery (sqlselect);
textArea1.append(sqlselect + "\n");
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement)conn.prepareStatement(sqlselect);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i = 1; i <= col; i++) {
str = (rs.getString(i)+ "\t");
if ((i == 3) && (rs.getString(i).length() < 8)) {
textArea1.append("\t");
str = str + "\n";
}
textArea1.append(str);
wbw.write(str);
}
}
} catch (SQLException e) {
}
} catch (Exception e) {
} finally {
br.close();
wbw.close();
}
} catch (SQLException se){
} catch (Exception e){
} finally {
try{
if(stmt!=null)
conn.close();
} catch (SQLException se){
}
try{
if(conn!=null)
conn.close();
} catch (SQLException se){
}
long endTime = System.currentTimeMillis();
textArea1.append((endTime - startTime) / 1000D + "s");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1User;
private javax.swing.JButton jButton2Password;
private javax.swing.JButton jButton3Start;
private java.awt.Label label1User;
private java.awt.Label label2Password;
private java.awt.Label label3Start;
private static java.awt.TextArea textArea1;
private java.awt.TextField text1User;
private java.awt.TextField text2Password;
// End of variables declaration
}