我有一个学校项目,我必须使用Netbeans创建一个注册和登录GUI,然后将其与SQL数据库链接。我创建数据库和我的登录工作,但我正在努力与我的注册可能有人可以帮助我。这就是我到目前为止所做的。对于长代码帖子很抱歉,但我的错误可能就在这里。
public class Register extends javax.swing.JFrame {
Connection conn = null;
Statement stat = null;
ResultSet res = null;
public Register()
{
initComponents();
}
private Connection connect() {
// SQLite connection string
String url = "\"jdbc:derby://localhost:1527/MyDataBase\", \"LouwDataBase\", \"1234\"";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public void insert(String Email, String Password, String Name, String Surname, String Address, String Cell)
{
String sql = "INSERT INTO Table1Test(Email, Password, Name, Surname, Address, Cell) VALUES(?,?,?,?,?,?)";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, Email);
pstmt.setString(2, Password);
pstmt.setString(3, Name);
pstmt.setString(4, Surname);
pstmt.setString(5, Address);
pstmt.setString(6, Cell);
pstmt.executeUpdate();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jNameLbl = new javax.swing.JLabel();
jRegisterNextButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jNameLbl1 = new javax.swing.JLabel();
jNameLbl2 = new javax.swing.JLabel();
jNameLbl3 = new javax.swing.JLabel();
jNameLbl4 = new javax.swing.JLabel();
jNameLbl5 = new javax.swing.JLabel();
jNameLbl6 = new javax.swing.JLabel();
jNameRegisterField = new javax.swing.JTextField();
jAddressRegisterField = new javax.swing.JTextField();
jSurnameRegisterField = new javax.swing.JTextField();
jCellRegisterField = new javax.swing.JTextField();
jEmailRegisterField = new javax.swing.JTextField();
jCPasswordRegisterField = new javax.swing.JPasswordField();
jPasswordRegisterField = new javax.swing.JPasswordField();
jLabel3 = new javax.swing.JLabel();
jLabel1.setText("jLabel1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 0, 0));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setMaximumSize(new java.awt.Dimension(640, 480));
setMinimumSize(new java.awt.Dimension(640, 480));
setPreferredSize(new java.awt.Dimension(640, 480));
setResizable(false);
setSize(new java.awt.Dimension(640, 480));
getContentPane().setLayout(null);
jNameLbl.setBackground(new java.awt.Color(102, 102, 102));
jNameLbl.setFont(new java.awt.Font("Bradley Hand ITC", 1, 24)); // NOI18N
jNameLbl.setForeground(new java.awt.Color(255, 255, 255));
jNameLbl.setText("Name");
jNameLbl.setMaximumSize(new java.awt.Dimension(100, 30));
jNameLbl.setMinimumSize(new java.awt.Dimension(100, 30));
jNameLbl.setPreferredSize(new java.awt.Dimension(100, 30));
getContentPane().add(jNameLbl);
jNameLbl.setBounds(10, 99, 230, 30);
jRegisterNextButton.setBackground(new java.awt.Color(102, 102, 102));
jRegisterNextButton.setFont(new java.awt.Font("Bradley Hand ITC", 1, 24)); // NOI18N
jRegisterNextButton.setForeground(new java.awt.Color(255, 255, 255));
jRegisterNextButton.setText("Next");
jRegisterNextButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRegisterNextButtonActionPerformed(evt);
}
});
getContentPane().add(jRegisterNextButton);
jRegisterNextButton.setBounds(10, 410, 100, 35);
jLabel2.setBackground(new java.awt.Color(102, 102, 102));
jLabel2.setFont(new java.awt.Font("Bradley Hand ITC", 1, 48)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Register");
getContentPane().add(jLabel2);
jLabel2.setBounds(10, 11, 620, 77);
jNameLbl1.setBackground(new java.awt.Color(102, 102, 102));
jNameLbl1.setFont(new java.awt.Font("Bradley Hand ITC", 1, 24)); // NOI18N
jNameLbl1.setForeground(new java.awt.Color(255, 255, 255));
jNameLbl1.setText("Email");
jNameLbl1.setMaximumSize(new java.awt.Dimension(100, 30));
jNameLbl1.setMinimumSize(new java.awt.Dimension(100, 30));
jNameLbl1.setPreferredSize(new java.awt.Dimension(100, 30));
getContentPane().add(jNameLbl1);
jNameLbl1.setBounds(10, 190, 230, 30);
jNameLbl2.setBackground(new java.awt.Color(255, 255, 255));
jNameLbl2.setFont(new java.awt.Font("Bradley Hand ITC", 1, 24)); // NOI18N
jNameLbl2.setForeground(new java.awt.Color(255, 255, 255));
jNameLbl2.setText("Address");
jNameLbl2.setMaximumSize(new java.awt.Dimension(100, 30));
jNameLbl2.setMinimumSize(new java.awt.Dimension(100, 30));
jNameLbl2.setPreferredSize(new java.awt.Dimension(100, 30));
getContentPane().add(jNameLbl2);
jNameLbl2.setBounds(10, 230, 230, 30);
jNameLbl3.setBackground(new java.awt.Color(102, 102, 102));
jNameLbl3.setFont(new java.awt.Font("Bradley Hand ITC", 1, 24)); // NOI18N
jNameLbl3.setForeground(new java.awt.Color(255, 255, 255));
jNameLbl3.setText("Surname");
jNameLbl3.setMaximumSize(new java.awt.Dimension(130, 30));
jNameLbl3.setMinimumSize(new java.awt.Dimension(130, 30));
jNameLbl3.setPreferredSize(new java.awt.Dimension(130, 30));
getContentPane().add(jNameLbl3);
jNameLbl3.setBounds(10, 140, 230, 30);
jNameLbl4.setBackground(new java.awt.Color(255, 255, 255));
jNameLbl4.setFont(new java.awt.Font("Bradley Hand ITC", 1, 24)); // NOI18N
jNameLbl4.setForeground(new java.awt.Color(255, 255, 255));
jNameLbl4.setText("Cell");
jNameLbl4.setMaximumSize(new java.awt.Dimension(100, 30));
jNameLbl4.setMinimumSize(new java.awt.Dimension(100, 30));
jNameLbl4.setPreferredSize(new java.awt.Dimension(100, 30));
getContentPane().add(jNameLbl4);
jNameLbl4.setBounds(10, 270, 230, 30);
jNameLbl5.setBackground(new java.awt.Color(255, 255, 255));
jNameLbl5.setFont(new java.awt.Font("Bradley Hand ITC", 1, 24)); // NOI18N
jNameLbl5.setForeground(new java.awt.Color(255, 255, 255));
jNameLbl5.setText("Password");
jNameLbl5.setMaximumSize(new java.awt.Dimension(100, 30));
jNameLbl5.setMinimumSize(new java.awt.Dimension(100, 30));
jNameLbl5.setPreferredSize(new java.awt.Dimension(100, 30));
getContentPane().add(jNameLbl5);
jNameLbl5.setBounds(10, 310, 200, 30);
jNameLbl6.setBackground(new java.awt.Color(102, 102, 102));
jNameLbl6.setFont(new java.awt.Font("Bradley Hand ITC", 1, 24)); // NOI18N
jNameLbl6.setForeground(new java.awt.Color(255, 255, 255));
jNameLbl6.setText("Confirm Password");
jNameLbl6.setMaximumSize(new java.awt.Dimension(100, 30));
jNameLbl6.setMinimumSize(new java.awt.Dimension(100, 30));
jNameLbl6.setPreferredSize(new java.awt.Dimension(100, 30));
getContentPane().add(jNameLbl6);
jNameLbl6.setBounds(10, 346, 230, 30);
jNameRegisterField.setBackground(new java.awt.Color(102, 102, 102));
jNameRegisterField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jNameRegisterFieldActionPerformed(evt);
}
});
getContentPane().add(jNameRegisterField);
jNameRegisterField.setBounds(250, 100, 350, 30);
jAddressRegisterField.setBackground(new java.awt.Color(102, 102, 102));
jAddressRegisterField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jAddressRegisterFieldActionPerformed(evt);
}
});
getContentPane().add(jAddressRegisterField);
jAddressRegisterField.setBounds(250, 221, 350, 30);
jSurnameRegisterField.setBackground(new java.awt.Color(102, 102, 102));
jSurnameRegisterField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jSurnameRegisterFieldActionPerformed(evt);
}
});
getContentPane().add(jSurnameRegisterField);
jSurnameRegisterField.setBounds(250, 140, 350, 30);
jCellRegisterField.setBackground(new java.awt.Color(102, 102, 102));
jCellRegisterField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCellRegisterFieldActionPerformed(evt);
}
});
getContentPane().add(jCellRegisterField);
jCellRegisterField.setBounds(250, 260, 350, 30);
jEmailRegisterField.setBackground(new java.awt.Color(102, 102, 102));
jEmailRegisterField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jEmailRegisterFieldActionPerformed(evt);
}
});
getContentPane().add(jEmailRegisterField);
jEmailRegisterField.setBounds(250, 180, 350, 30);
jCPasswordRegisterField.setBackground(new java.awt.Color(102, 102, 102));
jCPasswordRegisterField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCPasswordRegisterFieldActionPerformed(evt);
}
});
getContentPane().add(jCPasswordRegisterField);
jCPasswordRegisterField.setBounds(250, 340, 350, 30);
jPasswordRegisterField.setBackground(new java.awt.Color(102, 102, 102));
jPasswordRegisterField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordRegisterFieldActionPerformed(evt);
}
});
getContentPane().add(jPasswordRegisterField);
jPasswordRegisterField.setBounds(250, 300, 350, 30);
jLabel3.setIcon(new javax.swing.ImageIcon("D:\\Documents\\NetBeansProjects\\IT PAT\\PAT Images\\safari_sunset_2512555k1.jpg")); // NOI18N
jLabel3.setText("jLabel3");
getContentPane().add(jLabel3);
jLabel3.setBounds(0, 0, 640, 480);
pack();
}// </editor-fold>
private void jNameRegisterFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jSurnameRegisterFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jPasswordRegisterFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jAddressRegisterFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jRegisterNextButtonActionPerformed(java.awt.event.ActionEvent evt) {
Register Reg = new Register();
Reg.insert(jEmailRegisterField.getText(), jPasswordRegisterField.getText(), jNameRegisterField.getText(),
jSurnameRegisterField.getText(), jAddressRegisterField.getText(), jCellRegisterField.getText());
}
private void jCPasswordRegisterFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jCellRegisterFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jEmailRegisterFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Register().setVisible(true);
}
});
}
}
答案 0 :(得分:1)
我认为你的问题在你的网址中,你不应该像你一样设置你的网址:
String url = "\"jdbc:derby://localhost:1527/MyDataBase\", \"LouwDataBase\", \"1234\"";
您必须将网址,用户名和密码分开,如下所示:
private Connection connect() {
// SQLite connection string
String url = "jdbc:derby://localhost:1527/MyDataBase";
String username = "LouwDataBase";
String password = "1234";
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}