我有一个连接到数据库的应用程序,如果名称不存在则创建一个表,并添加新的注册用户信息,以便我可以将密码哈希到我的登录框架,并将它们与数据库也将被散列。目前我有以下错误代码。
SQLException: A table must have at least 1 column
SQLState: 42000
VendorError: 1113
使用此代码段
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoadDriver {
public static void sqlDriver(String[] args) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = null;
conn =
DriverManager
.getConnection(
"jdbc:mysql://dbname.dbwebsite.com:3306",
"user", "H45H3DP455W0RD");
java.sql.PreparedStatement pstmt = conn
.prepareStatement("USE db_name");
java.sql.PreparedStatement pstmt2 = conn.
prepareStatement("CREATE TABLE IF NOT EXISTS db_table");
java.sql.PreparedStatement pstmt3 = conn.prepareStatement("INSERT INTO db_table (user, pass, repass, name, lame, dob, gender, adds, apt, city, zip, phone, email) VALUES(id int NOT NULL auto_increment, VARCHAR(30) user, VARCHAR(30) pass, VARCHAR(30) repass, VARCHAR(30) name, VARCHAR(30) lname, VARCHAR(10) dob, TEXT gender, TEXT addr, TEXT apt, TEXT city, TEXT zip, TEXT phone, VARCHAR(30) email, PRIMARY KEY(id));");
pstmt.executeUpdate();
pstmt2.executeUpdate();
pstmt3.executeUpdate();
ResultSet rs = pstmt.executeQuery();
rs.close();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}
在主要班级
JButton registerSend = new JButton("Submit");
regFrame.add(registerSend, BorderLayout.SOUTH);
registerSend
.addActionListener(new ActionListener() {
@Override
public void actionPerformed(
ActionEvent r) {
JFrame gameFrame = new JFrame(
"Welcome to Heavy Evil");
gameFrame.setBounds(0, 0, 1200,
800);
gameFrame.setVisible(true);
int id;
String user = tagText.getText();
char[] pass = passText.getPassword();
char[] repass = passConfirm
.getPassword();
String name = heyText.getText();
String lname = heyText2
.getText();
String dob = userAge.getText();
String gender = genDer
.getText();
String addr = heyText3
.getText();
String apt = heyText4.getText();
String city = heyText5
.getText();
String zip = heyText6.getText();
String phone = heyText7
.getText();
String email = heyText8
.getText();
try {
LoadDriver.sqlDriver(null);
} catch (InstantiationException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
}
}
});
答案 0 :(得分:2)
CREATE TABLE IF NOT EXISTS db_table
您尚未指定任何列,这正是错误消息告诉您的内容。正确的语法是
CREATE TABLE IF NOT EXISTS db_table (
column_name1 datatype,
column_name2 datatype,
etc.
)