我正在创建一个“注册”表单,我已完成所有操作,但仍然无法连接。我希望这里有人可以提供帮助。
我的myConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class myConnection {
public static Connection getConnection() {
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://turtlenetwork.net/contact" + "user=IDKLMAO&password=OkIKnowYouTriedButNOLOL");
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
return con;
}
}
注册
private void jLabelCreateMouseClicked(java.awt.event.MouseEvent evt) {
Connection con = myConnection.getConnection();
PreparedStatement ps;
try {
ps = con.prepareStatement("INSERT INTO `user`(`username`, `pass`, `mail`) VALUES (?,?,?)");
ps.setString(1, jTextField1.getText());
ps.setString(2, String.valueOf(jPasswordField1.getPassword()));
ps.setString(3, jTextField3.getText());
if (ps.executeUpdate() != 0) {
JOptionPane.showMessageDialog(null, "Account Created");
} else {
JOptionPane.showMessageDialog(null, "Failed");
}
} catch (HeadlessException | SQLException ex) {
Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是我运行时遇到的错误,单击“创建”按钮。
应该将所有内容写入数据库并创建该帐户。
答案 0 :(得分:0)
Connect Java to a MySQL database
https://www.javatpoint.com/example-to-connect-to-the-mysql-database
每当在Java中建立连接时,都需要确保在用户名和密码中添加两个逗号。
con = DriverManager.getConnection("jdbc:mysql://turtlenetwork.net/contact", "username", "password");
确保用逗号分隔用户名和密码。
另外,请在此处查看DriverManager文档
https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html
答案 1 :(得分:0)
我设法自己解决了这个问题。 这是新的myConnection.java
$value=["wccpf_carton_weight_1"]
然后还有我也更改过的SignUp。
package ContactList;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
public class myConnection {
// Init Database Constants
private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/contact";
private static final String USERNAME = "NOPE";
private static final String PASSWORD = "NOPE";
private static final String MAX_POOL = "250";
// Init Connection Object
private Connection connection;
// Init Properties Object
private Properties properties;
// Create Properties
private Properties getProperties() {
if (properties == null) {
properties = new Properties();
properties.setProperty("user", USERNAME);
properties.setProperty("password", PASSWORD);
properties.setProperty("MaxPooledStatements", MAX_POOL);
}
return properties;
}
// Connect Database
public Connection connect() {
if (connection == null) {
try {
Class.forName(DATABASE_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL, getProperties());
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
return connection;
}
// Disconnect Database
public void disconnect() {
if (connection != null) {
try {
connection.close();
connection = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
PreparedStatement prepareStatement(String insert_into_userusername_pass_mail_VALUES) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
感谢所有尝试提供帮助的人。