代码是这样的:
import java.awt.EventQueue;
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.JPasswordField;
import javax.swing.JTextField;
public class AddUser extends JFrame { /**
* Launch the application
* @param args
*/
/**
* Create the frame
*/
String[] a=new String[3];
public AddUser() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setTitle("添加用户");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel label = new JLabel();
label.setText("用户 ID");
label.setBounds(39, 54, 66, 18);
getContentPane().add(label); final JLabel label_1 = new JLabel();
label_1.setText("用 户 名");
label_1.setBounds(39, 93, 66, 18);
getContentPane().add(label_1); final JLabel label_2 = new JLabel();
label_2.setText("密 码");
label_2.setBounds(39, 137, 66, 18);
getContentPane().add(label_2); final JLabel label_3 = new JLabel();
label_3.setText("重复密码");
label_3.setBounds(39, 178, 66, 18);
getContentPane().add(label_3);
final JTextField textField = new JTextField();
textField.setBounds(136, 52, 159, 22);
getContentPane().add(textField);
//ID final JTextField textField_1 = new JTextField();
textField_1.setBounds(136, 91, 159, 22);
getContentPane().add(textField_1);
//name final JPasswordField passwordField = new JPasswordField();
passwordField.setBounds(136, 135, 159, 22);
getContentPane().add(passwordField);
//password
final JPasswordField passwordField_1 = new JPasswordField();
passwordField_1.setBounds(136, 176, 159, 22);
getContentPane().add(passwordField_1)
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e)
{
if(e.getActionCommand().equals("添加")) ;
{
a[0]=textField.getText();
a[1]=textField_1.getText();
a[2]=passwordField.getText();
String sql="insert into usert values('"+a[1]+"','"+a[2]+"','"+a[0]+"') "
System.out.print(sql);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:huji","sa","");
System.out.print("aaaaaaaaaa");
Statement stmt = con.createStatement();
if(con!=null)
{
stmt.executeUpdate(sql);
new into();
}
stmt.close();
con.close();
}
catch(ClassNotFoundException a)
{}
catch(SQLException a)
{}
}
}}
);
button.setText("添加");
button.setBounds(79, 258, 106, 28);
getContentPane().add(button); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener()
{
public void actionPerformed(final ActionEvent e)
{ if(e.getActionCommand().equals("取消"))
setVisible(false);
}
});
button_1.setText("取消");
button_1.setBounds(242, 258, 106, 28);
getContentPane().add(button_1);
setVisible(true);//
}
}
拜托了,谢谢啊~~
import java.awt.EventQueue;
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.JPasswordField;
import javax.swing.JTextField;
public class AddUser extends JFrame { /**
* Launch the application
* @param args
*/
/**
* Create the frame
*/
String[] a=new String[3];
public AddUser() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setTitle("添加用户");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel label = new JLabel();
label.setText("用户 ID");
label.setBounds(39, 54, 66, 18);
getContentPane().add(label); final JLabel label_1 = new JLabel();
label_1.setText("用 户 名");
label_1.setBounds(39, 93, 66, 18);
getContentPane().add(label_1); final JLabel label_2 = new JLabel();
label_2.setText("密 码");
label_2.setBounds(39, 137, 66, 18);
getContentPane().add(label_2); final JLabel label_3 = new JLabel();
label_3.setText("重复密码");
label_3.setBounds(39, 178, 66, 18);
getContentPane().add(label_3);
final JTextField textField = new JTextField();
textField.setBounds(136, 52, 159, 22);
getContentPane().add(textField);
//ID final JTextField textField_1 = new JTextField();
textField_1.setBounds(136, 91, 159, 22);
getContentPane().add(textField_1);
//name final JPasswordField passwordField = new JPasswordField();
passwordField.setBounds(136, 135, 159, 22);
getContentPane().add(passwordField);
//password
final JPasswordField passwordField_1 = new JPasswordField();
passwordField_1.setBounds(136, 176, 159, 22);
getContentPane().add(passwordField_1)
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e)
{
if(e.getActionCommand().equals("添加")) ;
{
a[0]=textField.getText();
a[1]=textField_1.getText();
a[2]=passwordField.getText();
String sql="insert into usert values('"+a[1]+"','"+a[2]+"','"+a[0]+"') "
System.out.print(sql);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:huji","sa","");
System.out.print("aaaaaaaaaa");
Statement stmt = con.createStatement();
if(con!=null)
{
stmt.executeUpdate(sql);
new into();
}
stmt.close();
con.close();
}
catch(ClassNotFoundException a)
{}
catch(SQLException a)
{}
}
}}
);
button.setText("添加");
button.setBounds(79, 258, 106, 28);
getContentPane().add(button); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener()
{
public void actionPerformed(final ActionEvent e)
{ if(e.getActionCommand().equals("取消"))
setVisible(false);
}
});
button_1.setText("取消");
button_1.setBounds(242, 258, 106, 28);
getContentPane().add(button_1);
setVisible(true);//
}
}
拜托了,谢谢啊~~