java运行问题,看好像没问题,
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JTextArea;
public class Dqq {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
JTextArea textArea;
protected String STR,STR_1,STR_2,STR_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Dqq window = new Dqq();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Dqq() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(26, 21, 94, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(135, 21, 85, 21);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(26, 61, 94, 21);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(134, 61, 86, 21);
frame.getContentPane().add(textField_3);
textField_3.setColumns(10);
JButton button = new JButton("\u589E\u52A0");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DBConnection s=new DBConnection();
STR=textField.getText().trim();
STR_1=textField_1.getText().trim();
STR_2=textField_2.getText().trim();
STR_3=textField_3.getText().trim();
s.insert(STR,STR_1,STR_2,STR_3); //增加
if(s.i==1)
textArea.append("添加成功");
textArea.append("\n");
}
});
button.setBounds(230, 20, 93, 23);
frame.getContentPane().add(button);
JButton button_1 = new JButton("\u5220\u9664");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DBConnection ss=new DBConnection();
STR_1=textField_1.getText().trim();//删除
ss.delete(STR_1);
if(ss.k==1)
textArea.append("删除成功");
textArea.append("\n");
}
});
button_1.setBounds(331, 20, 93, 23);
frame.getContentPane().add(button_1);
JButton button_2 = new JButton("\u67E5\u627E");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //查找
DBConnection sss=new DBConnection();
Student student=new Student();
STR=textField_1.getText().trim();
sss.select(STR);
textArea.append("年龄"+student.age+"id"+student.id+"信息"+student.message+"名字"+student.name);
textArea.append("\n");
}
});
button_2.setBounds(230, 60, 93, 23);
frame.getContentPane().add(button_2);
JButton button_3 = new JButton("\u66F4\u65B0");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //更新
DBConnection ssss=new DBConnection();
STR=textField.getText().trim();
STR_1=textField_1.getText().trim();
STR_2=textField_2.getText().trim();
STR_3=textField_3.getText().trim();
ssss.update(STR,STR_1,STR_2,STR_3);
if(ssss.j==1)
textArea.append("更新成功");
textArea.append("\n");
}
});
button_3.setBounds(331, 60, 93, 23);
frame.getContentPane().add(button_3);
JTextArea textArea = new JTextArea();
textArea.setBounds(26, 124, 290, 128);
frame.getContentPane().add(textArea);
}
}
public class DBConnection {
private String dbDriver="com.mysql.jdbc.Driver";
private String dbUrl="jdbc:mysql://localhost:3306/student";
private String dbUser="root";
private String dbPass="123456789";
int i=0 ;
int j=0;
int k=0;
int l=0;
public Connection getConn()
{
Connection conn=null;
try
{
Class.forName(dbDriver);
System.out.println("JBDC 加载成功!");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
conn = DriverManager.getConnection(dbUrl,dbUser,dbPass);
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public int insert(String a,String b,String c,String d) //增加
{
String sql="insert into test values(?,?,?,?)";
Connection cnn=getConn();
try{
PreparedStatement preStmt =cnn.prepareStatement(sql);
preStmt.setString(1,a);
preStmt.setString(1,b);
preStmt.setString(1,c);
preStmt.setString(1,d);
i=preStmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
return i;
}
public int update(String a,String b,String c,String d)
{
String sql="update test set age=?,message=? where name=c";
Connection cnn=getConn();
try{
PreparedStatement preStmt =cnn.prepareStatement(sql);
preStmt.setString(1,a);
preStmt.setString(2,c);
j=preStmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
return j;
}
public int delete(String c)
{
String sql = "delete from test where name=a";
Connection conn = getConn();
try
{
java.sql.Statement stmt = conn.createStatement();
k = stmt.executeUpdate(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
return k;
}
public int select(String c) //查找
{
Student Stu=new Student();
String sql = "select * from test where name=a";
try
{
Connection conn = getConn();
java.sql.Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
Stu.age = rs.getString(1);//
Stu.id= rs.getString(2);
Stu.message= rs.getString(3);
Stu.name= rs.getString(4);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return l;
}
}