2014-02-11 71 views
0

我有一個代碼在'mysql'數據庫中輸入一些數據到'createaccount'表中。我使用NetBeans IDE創建了它。表和數據庫都被創建。每當我運行代碼時,都會顯示「沒有選擇數據庫」的SQLException。我將在下面提供我的代碼。爲什麼會顯示此錯誤?我應該怎麼做才能解決它?爲什麼「沒有選擇數據庫」SQLException顯示

package login; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.geometry.Insets; 
import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.Label; 
import javafx.scene.control.PasswordField; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.stage.Stage; 

public class Login extends Application { 

TextField t1,t2; 
PasswordField t3; 
ComboBox comboBox2; 

private Connection connect = null; 
private Statement statement = null; 
private PreparedStatement preparedStatement = null; 

@Override 
public void start(Stage stage) { 

    BorderPane border = new BorderPane(); 

    border.setTop(loginHBox1()); 
    border.setLeft(loginVBox1()); 
    border.setRight(loginVBox2()); 

    Scene scene = new Scene(border,700,450); 
    stage.setScene(scene); 
    stage.setResizable(false); 
    scene.getStylesheets().add 
    (Login.class.getResource("Login.css").toExternalForm()); 
    stage.show(); 
} 

private HBox loginHBox1() { 

    HBox hbox = new HBox(); 
    hbox.setPadding(new Insets(15, 12, 10, 180)); 
    hbox.setSpacing(10); // Gap between nodes 

    Label lb1=new Label("LOG IN OR CREATE NEW ACCOUNT"); 
    lb1.setAlignment(Pos.CENTER); 
    lb1.setFont(Font.font("Calibri",FontWeight.BOLD,26)); 
    lb1.setTextFill(Color.BLACK); 

    hbox.getChildren().addAll(lb1); 

    return hbox; 
} 

private VBox loginVBox1() { 

    VBox hbox = new VBox(); 
    hbox.setPadding(new Insets(20,30,15,50)); // Set all sides to 10 
    hbox.setSpacing(10);  // Gap between nodes 

    Label lb3=new Label("LOG IN"); 
    lb3.setAlignment(Pos.CENTER); 
    lb3.setFont(Font.font("Calibri",FontWeight.BOLD,24)); 
    lb3.setTextFill(Color.BLACK); 

    Label lb1=new Label("Username"); 
    lb1.setAlignment(Pos.CENTER); 
    lb1.setFont(Font.font("Calibri",FontWeight.BOLD,16)); 
    lb1.setTextFill(Color.BLACK); 

    TextField t11=new TextField(); 
    t11.setPrefSize(150,30); 

    Label lb2=new Label("Password"); 
    lb2.setAlignment(Pos.CENTER); 
    lb2.setFont(Font.font("Calibri",FontWeight.BOLD,16)); 
    lb2.setTextFill(Color.BLACK); 

    PasswordField pw11=new PasswordField(); 
    pw11.setPrefSize(150,30); 

    Button b1=new Button("LOG IN"); 
    b1.setFont(Font.font("Calibri",FontWeight.BOLD,16)); 
    b1.setPrefSize(80,5); 

    hbox.getChildren().addAll(lb3,lb1,t11,lb2,pw11,b1); 

    return hbox; 
} 

private VBox loginVBox2() 
{ 
    VBox hbox1 = new VBox(); 
    hbox1.setPadding(new Insets(15, 50, 15, 10)); 
    hbox1.setSpacing(10); 

    Label lb4=new Label("CREATE NEW ACCOUNT"); 
    lb4.setFont(Font.font("Calibri",FontWeight.BOLD,24)); 
    lb4.setPrefSize(250,30); 
    lb4.setTextFill(Color.BLACK); 

    Label lb1=new Label("Full Name "); 
    lb1.setFont(Font.font("Calibri",FontWeight.BOLD,18)); 
    lb1.setPrefSize(100, 30); 
    lb1.setTextFill(Color.BLACK); 

    t1=new TextField(); 
    t1.setPrefSize(50,30); 

    Label lb2=new Label("User Name "); 
    lb2.setFont(Font.font("Calibri",FontWeight.BOLD,18)); 
    lb2.setPrefSize(150, 30); 
    lb2.setTextFill(Color.BLACK); 

    t2=new TextField(); 
    t2.setPrefSize(100,30); 

    Label lb3=new Label("Password "); 
    lb3.setFont(Font.font("Calibri",FontWeight.BOLD,18)); 
    lb3.setPrefSize(150, 30); 
    lb3.setTextFill(Color.BLACK); 

    t3=new PasswordField(); 
    t3.setPrefSize(100,30); 

    Label lb5=new Label("Gender "); 
    lb5.setFont(Font.font("Calibri",FontWeight.BOLD,18)); 
    lb5.setPrefSize(150, 30); 
    lb5.setTextFill(Color.BLACK); 

    ObservableList<String> options2 = 
    FXCollections.observableArrayList(
    "Male","Female"); 
    comboBox2 = new ComboBox(options2); 
    comboBox2.setPrefSize(250,30); 

    Button btn1=new Button("CREATE"); 
    btn1.setFont(Font.font("Calibri",FontWeight.BOLD,18)); 
    btn1.setPrefSize(100,30); 

    btn1.setOnAction(new EventHandler<ActionEvent>() { 
    @Override 
    public void handle(ActionEvent e) { 
     try { 
      createAccount(); 
     } catch ( ClassNotFoundException | SQLException ex) { 
      Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
    }); 


    hbox1.getChildren().addAll(lb4,lb1,t1,lb2,t2,lb3,t3,lb5,comboBox2,btn1); 
    return hbox1; 
} 

public void createAccount() throws ClassNotFoundException, SQLException 
{ 
try { 
    // This will load the MySQL driver, each DB has its own driver 
    Class.forName("com.mysql.jdbc.Driver"); 
    // Setup the connection with the DB 
    connect = DriverManager 
     .getConnection("jdbc:mysql://localhost:3306/mysql?" 
      + "user=root&password=virus"); 

    // Statements allow to issue SQL queries to the database 
    statement = connect.createStatement(); 

    // PreparedStatements can use variables and are more efficient 
    preparedStatement = connect 
     .prepareStatement("insert into createaccount values (?, ?, ?, ?)"); 
    // "myuser, webpage, datum, summary, COMMENTS from FEEDBACK.COMMENTS"); 
    // Parameters start with 1 
    preparedStatement.setString(1, "Tomin Jacob"); 
    preparedStatement.setString(2, "Tom333"); 
    preparedStatement.setString(3, "pass"); 
    preparedStatement.setString(4, "male"); 

    preparedStatement.executeUpdate(); 
} 

catch (ClassNotFoundException | SQLException e) { 
    throw e; 
} finally { 
    close(); 
} 

} 

private void close() { 
try { 


    if (statement != null) { 
    statement.close(); 
    } 

    if (connect != null) { 
    connect.close(); 
    } 
} catch (SQLException e) { 

} 
} 

public static void main(String[] args) 
{ 
    launch(args); 
} 

} 
+0

-1,用於發佈很多與您的問題無關的代碼。請將代碼簡化爲重現此問題所需的內容。 –

+0

@MarkRotteveel好的...我會在下次做。 – TomJ

回答

1

「com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重複的項目‘Tom33’關鍵‘PRIMARY’」

這意味着你的Tom33條目已被插入到數據庫或已經存在和你試圖再次插入相同的值。主鍵約束限制我們有重複的值。

2

取而代之的是

insert into createaccount values (?, ?, ?, ?) 

嘗試

insert into YorDbName.createaccount values (?, ?, ?, ?) 

替換YorDbName用你的數據庫名稱。

+0

現在顯示另一個異常 - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重複條目'Tom33'爲'PRIMARY' – TomJ

0

變化,

try { 

} catch (ClassNotFoundException | SQLException e) { 


} 

try { 

} catch (SQLException e) { 


} catch (ClassNotFoundException e) { 

} 

,並更改

statement = connect.createStatement();

preparedStatement = connect.createStatement(); 
相關問題