67,512
社区成员
发帖
与我相关
我的任务
分享
try (//你这是括号
Connection conn = DriverManager.getConnection(url, user, pass);
PreparedStatement pstmt = conn.prepareStatement(sq)) {//你的花括号在这里 汗 eclipse能编译通过?
// 获取密码域的内容
pstmt.setString(1, uuu);
pstmt.setString(2, ppp);
// pstmt.executeQuery(sq);
pstmt.executeUpdate(sq) ;
if (pas.getPassword() == pas2.getPassword() ) {//还有字符串比较是pas!=null && pas.getPassword().equals(pas2.getPassword())问题很多啊
insert("uuu", "ppp");
// JOptionPane.showMessageDialog(jf, "添加数据成功");
return;
}
} catch (SQLException ex) {
System.out.println("插入数据发生异常:" + ex.getMessage());
}
package com.boyu.util;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Addvalue {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://127.0.0.1:3306/luke";
private String user = "root";
private String pass = "671792";
// private String sql ="insert into jdbc_test values(?,?)";
private JFrame jf = new JFrame("添加数据窗口");
private JLabel us = new JLabel("请输入用户名");
private JTextField use = new JTextField(15);
private JLabel ps = new JLabel("请输入密码");
private JPasswordField pas = new JPasswordField(15);
private JLabel pps = new JLabel("确认密码");
private JPasswordField pas2 = new JPasswordField(15);
private JButton inse = new JButton("确定添加数据");
private JButton reset = new JButton("重置信息");
/**
* 创建添加窗口
*/
public void init() throws Exception {
Class.forName(driver);
jf.setLayout(new GridLayout(4, 2, 1, 1));
jf.add(us);
jf.add(use);
jf.add(ps);
jf.add(pas);
jf.add(pps);
jf.add(pas2);
jf.add(reset);
jf.add(inse);
jf.setBounds(500, 400, 260, 152);
// jf.setSize(260, 152);
jf.setVisible(true);
inse.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (use.getText().length() == 0) {
JOptionPane.showMessageDialog(jf, "请检查输入");
} else if (use.getText().length() != 0
&& pas.getPassword() != null) {
insert(use.getText(), pas.getPassword().toString());
JOptionPane.showMessageDialog(jf, "添加数据成功");
use.setText("");
pas.setText("");
pas2.setText("");
}
}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
use.setText("");
pas.setText("");
pas2.setText("");
}
});
}
public void insert(String uuu, String ppp) {
char[] s = null;
s = pas.getPassword();
uuu = use.getText();
ppp = new String(s);
String sq = "insert into zhuce values(?,?)";
// String sq ="insert into jdbc_test values(uuu,ppp)";
try {
Connection conn = DriverManager.getConnection(url, user, pass);
PreparedStatement pstmt = conn.prepareStatement(sq);
// 获取密码域的内容
pstmt.setString(1, uuu);
pstmt.setString(2, ppp);
// pstmt.executeQuery(sq);
pstmt.executeUpdate(sq);
} catch (SQLException ex) {
System.out.println("插入数据发生异常:" + ex.getMessage());
System.exit(0) ;
}
}
public static void main(String[] args) throws Exception {
(new Addvalue()).init();
}
}
package com.boyu.util;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Addvalue {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://127.0.0.1:3306/luke";
private String user = "root";
private String pass = "671792";
// private String sql ="insert into jdbc_test values(?,?)";
private JFrame jf = new JFrame("添加数据窗口");
private JLabel us = new JLabel("请输入用户名");
private JTextField use = new JTextField(15);
private JLabel ps = new JLabel("请输入密码");
private JPasswordField pas = new JPasswordField(15);
private JLabel pps = new JLabel("确认密码");
private JPasswordField pas2 = new JPasswordField(15);
private JButton inse = new JButton("确定添加数据");
private JButton reset = new JButton("重置信息");
/**
* 创建添加窗口
*/
public void init() throws Exception {
Class.forName(driver);
jf.setLayout(new GridLayout(4, 2, 1, 1));
jf.add(us);
jf.add(use);
jf.add(ps);
jf.add(pas);
jf.add(pps);
jf.add(pas2);
jf.add(reset);
jf.add(inse);
jf.setBounds(500, 400, 260, 152);
// jf.setSize(260, 152);
jf.setVisible(true);
inse.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (use.getText().length() == 0) {
JOptionPane.showMessageDialog(jf, "请检查输入");
} else if (use.getText().length() != 0
&& pas.getPassword() != null) {
insert("use", "pas");
JOptionPane.showMessageDialog(jf, "添加数据成功");
use.setText("");
pas.setText("");
pas2.setText("");
}
}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
use.setText("");
pas.setText("");
pas2.setText("");
}
});
}
public void insert(String uuu, String ppp) {
if (pas.getPassword().equals(pas2.getPassword())) {
char[] s = null;
s = pas.getPassword();
uuu = use.getText();
ppp = new String(s);
String sq = "insert into zhuce values(?,?)";
// String sq ="insert into jdbc_test values(uuu,ppp)";
try {
Connection conn = DriverManager.getConnection(url, user, pass);
PreparedStatement pstmt = conn.prepareStatement(sq);
// 获取密码域的内容
pstmt.setString(1, uuu);
pstmt.setString(2, ppp);
// pstmt.executeQuery(sq);
pstmt.executeUpdate(sq);
} catch (SQLException ex) {
System.out.println("插入数据发生异常:" + ex.getMessage());
return ;
}
}
}
public static void main(String[] args) throws Exception {
(new Addvalue()).init();
}
}