我想从用户那里获取文件路径并在数据中设置文件路径。我可以编写thre数据库,但缺少\
。这是代码;
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();
}
答案 0 :(得分:0)
好的,我使用预准备语句插入数据。它现在有效,工作代码如下。我分享它是因为有人可以需要它。
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();
}