gpt4 book ai didi

java - MySQL 删除\当我想在 java 中写入文件路径时

转载 作者:行者123 更新时间:2023-11-29 22:13:50 24 4
gpt4 key购买 nike

我想从用户那里获取文件路径并在数据中设置文件路径。我可以编写数据库,但 \ 丢失了。这是代码;

String username = jTextField2.getText();
String password = jTextField3.getText();
String name = jTextField9.getText();
String surname = jTextField10.getText();
boolean male = jRadioButton1.isSelected();
boolean female = jRadioButton2.isSelected();

try {
SetNewUserAvatar();
} catch (IOException | SQLException ex) {
Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, null, ex);
}

if("".equals(username) || "".equals(password) || "".equals(name) || "".equals(surname)){
JOptionPane.showMessageDialog(null, "Please, fill all the gaps");
}
else if( male == false && female == false ){
JOptionPane.showMessageDialog(null, "Please, select gender");
}

else {
DBConnect dbc = new DBConnect();
try {
dbc.registerUser(username, password);
} catch (SQLException ex) {
Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, null, ex);
}


try {
if( male==true ){
dbc.writeInfo(ProfilePanel.avatarPath[ProfilePanel.xx], name, surname, "male");
System.out.println(ProfilePanel.avatarPath[ProfilePanel.xx]);
}
else{
dbc.writeInfo(ProfilePanel.avatarPath[ProfilePanel.xx], name, surname, "female");
}
} catch (SQLException ex) {
Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, null, ex);
}

SignUp.super.dispose();
}

此代码位于另一个类中;

    public void writeInfo(String avatarpath, String name, String surname, String gender) throws SQLException {
String query = "select * from info";
rs = st.executeQuery(query);


while(rs.next()){
x = rs.getInt("id");
}

st.executeUpdate( "INSERT INTO info" + " (id, avatarpath, name, surname, gender) VALUES ('"+(x+1)+"','"+avatarpath+"', '"+name+"', '"+surname+"', '"+gender+"' )");
}

当我按下注册按钮时,我想看看我是否正确获取了路径,因此系统输出为 null
C:\Users\B_Ali\Documents\NetBeansProjects\JavaApplication20\build\classes\Users\shingeki_no_kyojin_by_asuka10-d6k762k.jpg

编辑:我从这个函数中获取输入。

public static void SetNewUserAvatar() throws IOException, SQLException{
DBConnect dbc = new DBConnect();
dbc.counter();
ProfilePanel.xx = dbc.x;
File file = jFileChooser1.getSelectedFile();
ProfilePanel.avatarPath[ProfilePanel.xx] = file.getAbsolutePath();
}

最佳答案

好的,我使用准备好的语句来插入数据。它现在可以工作了,工作代码如下。我分享它是因为有人可能需要它。

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
String username = jTextField2.getText();
String password = jTextField3.getText();
String name = jTextField9.getText();
String surname = jTextField10.getText();
boolean male = jRadioButton1.isSelected();
boolean female = jRadioButton2.isSelected();
Connection connection = null ;
PreparedStatement preparedStatement = null;
try {
SetNewUserAvatar();
} catch (IOException | SQLException ex) {
Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, null, ex);
}
if("".equals(username) || "".equals(password) || "".equals(name) || "".equals(surname)){
JOptionPane.showMessageDialog(null, "Please, fill all the gaps");
}
else if( male == false && female == false ){
JOptionPane.showMessageDialog(null, "Please, select gender");
}
else {
try{
Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");

}catch(Exception ex){
System.out.println("Error: " +ex);
}
DBConnect dbc = new DBConnect();
try {
dbc.registerUser(username, password);
} catch (SQLException ex) {
Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, null, ex);
}
try {
if( male==true ){
DBConnect.addInfo(connection, preparedStatement, ProfilePanel.avatarPath[ProfilePanel.xx], name, surname, "male");
}
else{
DBConnect.addInfo(connection, preparedStatement, ProfilePanel.avatarPath[ProfilePanel.xx], name, surname, "female");;
}
} catch (SQLException ex) {
Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, null, ex);
}
SignUp.super.dispose();
}
}

此代码在另一个类中。

 DBConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
st = con.createStatement();

Connection connection = null ;
PreparedStatement preparedStatement = null;

}catch(Exception ex){
System.out.println("Error: " +ex);
}
}
public static void addInfo(Connection connection,PreparedStatement preparedStatement, String avatarPath, String name, String surname, String gender) throws SQLException{
preparedStatement = connection.prepareStatement("INSERT INTO info (avatarpath, name, surname, gender) VALUES (?, ?, ?, ?)");
preparedStatement.setString(1, avatarPath);
preparedStatement.setString(2, name);
preparedStatement.setString(3, surname);
preparedStatement.setString(4, gender);
preparedStatement.executeUpdate();
}

关于java - MySQL 删除\当我想在 java 中写入文件路径时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31376536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com