我在 java 页面中有一个小程序来为我的应用程序创建数据库。这是代码。数据库位于 mysql。但对我不起作用。我认为我的代码是正确的。
private DataBaseSource dbSource = new DataBaseSourceImpl();
private Connection connection = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
/** Creates new form LoginScreen */
public LoginScreen() {
initComponents();
Container c =this.getContentPane();
c.setBackground(Color.WHITE);
connection = (Connection) dbSource.getConnection();
signInBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String uname = usrNameTxt.getText();
char[] pword = pwordTxt.getPassword();
String password = new String(pword);
if(uname.equals("")&& password.equals("")){
Util.showErrorMessageDialog("Please fill all the fields.");
}else{
if(uname.equals("") || uname.equals(" ")&& ! password.equals("")){
Util.showErrorMessageDialog("Login ID left blank");
}else{
if (password.equals("") || password.equals(" ")&& uname.equals("")) {
Util.showErrorMessageDialog("Password left blank.");
}else{
authenticateLogin();
}
}
}
}
});
}
public void authenticateLogin() {
try {
preparedStatement = (PreparedStatement) connection.prepareStatement("CREATE DATABASE IF NOT EXIST macfast");
preparedStatement.executeQuery();
} catch (SQLException ex) {
Logger.getLogger(LoginScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LoginScreen().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel2;
private javax.swing.JPasswordField pwordTxt;
private javax.swing.JButton signInBtn;
private javax.swing.JTextField usrNameTxt;
// End of variables declaration
}
这是我的代码示例。但它不会创建名为 macfast 的数据库...如上例所示。我的程序发生了什么。任何人请帮助我