简单的从MySQL里读取值,显示到JTextField里
/*
* 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 javaapplication35;
import java.awt.BorderLayout;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.sql.*;
/**
*
* @author 91152
*/
public class JavaApplication35 {
/**
* @param args the command line arguments
*/
JFrame jf = new JFrame();
JButton bt = new JButton("点击我");
JTextField jt1 = new JTextField(20);
JTextField jt2 = new JTextField(20);
Box box = Box.createVerticalBox();
String driver;
String url;
String user;
String pass;
String abcd;
String efgh;
ResultSet rs;
public void init(){
bt.addActionListener(e -> {
try{
rs.next();
abcd = rs.getString(2);
efgh = rs.getString(3);
}
catch(SQLException sq){
sq.printStackTrace();
}
jt1.setText(abcd);
jt2.setText(efgh);
});
jt1.setText(abcd);
jt2.setText(efgh);
box.add(bt);
box.add(jt1);
box.add(jt2);
jf.add(box);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public void getParam(String param) throws Exception{
Properties prop = new Properties();
prop.load(new FileReader(param));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
pass = prop.getProperty("pass");
}
public void createTable() throws Exception{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,pass);
Statement stat = conn.createStatement();
rs = stat.executeQuery("SELECT * FROM test_1.a333;");
rs.next();
abcd = rs.getString(2);
efgh = rs.getString(3);
System.out.println(abcd);
}
public static void main(String[] args) throws Exception {
JavaApplication35 ja = new JavaApplication35();
ja.getParam("mysql.ini");
ja.createTable();
ja.init();
}
}