MyFrame类用于实现数据的增删改查。
代码用JFrame进行了演示,其中的每一个按键都添加了事件监听。
这里假定mysql数据库的端口号是3306。
数据库是my_database。
表是user。
表中包含name和score两个元素。
使用前注意引入JDBC的jar包作为外部jar包。
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyFrame extends JFrame {
private static final int Width = 420;
private static final int Height = 300;
private static JFrame frame = null;
//private static Container ctn = null;
private static FlowLayout flowLayout = null;
private static JLabel nameLabel = null;
private static JLabel scoreLabel = null;
private static JTextField nameText = null;
private static JTextField scoreText = null;
private static JButton addButton = null;
private static JButton deleteButton = null;
private static JButton modifyButton = null;
private static JButton searchButton = null;
private static JLabel resultLabel = null;
private static JTextField resultText = null;
private static JButton closeButton = null;
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/my_database";
private static String userName = "root";
private static String password = "root";
private static Connection conn = null;
private static Statement stmt = null;
public MyFrame() {
frame = new JFrame("JFrame connect MySQL");
flowLayout = new FlowLayout(FlowLayout.LEFT);
flowLayout.setHgap(20);
flowLayout.setVgap(30);
frame.setLayout(flowLayout);
nameLabel = new JLabel("name");
scoreLabel = new JLabel("score");
nameText = new JTextField(10);
scoreText = new JTextField(10);
addButton = new JButton("ADD");
deleteButton = new JButton("DELETE");
modifyButton = new JButton("MODIFY");
searchButton = new JButton("SEARCH");
resultLabel = new JLabel("result");
resultText = new JTextField(30);
closeButton = new JButton("CLOSE");
frame.add(nameLabel);
frame.add(nameText);
frame.add(scoreLabel);
frame.add(scoreText);
frame.add(addButton);
frame.add(deleteButton);
frame.add(modifyButton);
frame.add(searchButton);
frame.add(resultLabel);
frame.add(resultText);
frame.add(closeButton);
addButton.addActionListener(new ButtonAction());
deleteButton.addActionListener(new ButtonAction());
modifyButton.addActionListener(new ButtonAction());
searchButton.addActionListener(new ButtonAction());
closeButton.addActionListener(new ButtonAction());
frame.setVisible(true);
frame.setSize(Width, Height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Object s = evt.getSource();
String name = nameText.getText();
String score = scoreText.getText();
if(s == addButton) {
if(name.length()==0 || score.length()==0) {
System.out.println("Please enter the name and score!");
}
else {
String sqlInsert =
"INSERT INTO my_table (name,score) VALUES (\"" + name + "\"," + score + ");";
try {
stmt.executeUpdate(sqlInsert);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Add " + name + " Succeed!");
}
} else if(s == deleteButton) {
if(name.length() == 0) {
System.out.println("You must enter the name");
return;
} else if(score.length() == 0) {
String sqlDelete = "DELETE FROM my_table WHERE name=\"" + name + "\";";
try {
stmt.executeUpdate(sqlDelete);
} catch (SQLException e) {
e.printStackTrace();
}
} else {
String sqlDelete = "DELETE FROM my_table WHERE name=\"" + name
+ "\" and score<=" + score + ";";
try {
stmt.executeUpdate(sqlDelete);
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("Delete succeed!");
} else if(s == modifyButton) {
if(name.length() == 0 || score.length() == 0) {
System.out.println("Please enter the name and score you want to modify!");
} else {
String sqlModify = "UPDATE my_table SET name=\"" + name
+ "\" WHERE score=" + score + ";";
try {
stmt.executeUpdate(sqlModify);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Modify " + name + " succeed!");
}
} else if(s == searchButton) {
String sqlName = " name=\"" + name + "\"";
String sqlScore = " score=" + score;
String sqlSelect = "SELECT * FROM my_table";
if(name.length() == 0 && score.length() == 0) {
;
} else if(score.length() == 0) {
sqlSelect += " WHERE" + sqlName + ";";
} else if(name.length() == 0) {
sqlSelect += " WHERE" + sqlScore + ";";
} else {
sqlSelect += " WHERE" + sqlName + " and" + sqlScore + ";";
}
//System.out.println(sqlSelect);
try {
ResultSet res = stmt.executeQuery(sqlSelect);
while(res.next()) {
String ansName = res.getString(1);
String ansScore = res.getString(2);
System.out.println(ansName + " get score: " + ansScore);
}
} catch (SQLException e) {
e.printStackTrace();
}
} else if(s == closeButton) {
try {
stmt.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
System.out.println("SQLException异常"+e.getMessage());
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
try {
Class.forName(driver);
System.out.println("数据库连接成功");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException");
}
try {
conn = DriverManager.getConnection(url, userName, password);
System.out.println("connect database successful");
stmt = conn.createStatement();
new MyFrame();
} catch (SQLException e) {
System.out.println("SQLException异常"+e.getMessage());
e.printStackTrace();
}
}
}
posted on 2015-03-08 16:43
marchalex 阅读(657)
评论(0) 编辑 收藏 所属分类:
java小程序