2016-12-12 3 views
0

準備されたstatement.Iを使用してデータベースにユーザーデータを追加する方法を作成しました。完全に機能します。次に、新しいものを追加するたびに、ユーザーがデータベースに入れます。たとえば、Submitボタンをクリックすると、ユーザーデータがデータベースに追加され、新しいフォルダがドキュメントフォルダに作成されています。すべての種類のコードを試しました.LinuxとNetBeansを使用しています8.1.Thanks in advanceデータベースに追加するたびに新しいファイルをファイルシステムに作成する方法

私の方法です。

public String addUser(int useId, String name, String lastName, String country, String birthplace, String adress, String password) throws SQLException { 
    List<User> list = listAllUsers(); 
    int size = list.size(); 



    Connection conn = null; 
    PreparedStatement ps = null; 
    ResultSet rs = null; 

    for (int i = 0; i < size; i++) { 
     if (useId == list.get(i).getIdUser()) { 
      return "error"; 
     } 

    } 

    conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/testBaza?user=root"); 
    ps = conn.prepareStatement("INSERT INTO User (id_user,name,last_name,birthplace,country,adress,user_pass) values(?,?,?,?,?,?,?)"); 
    conn.setAutoCommit(false); 

    ps.setInt(1, useId); 
    ps.setString(2, name); 
    ps.setString(3, lastName); 
    ps.setString(4, country); 
    ps.setString(5, birthplace); 
    ps.setString(6, adress); 
    ps.setString(7, password); 

    ps.executeUpdate(); 
    conn.commit(); 
    conn.close(); 



    return "userRegPage"; 

} 

答えて

0

以下のコードが有効です。プログラムを実行しているuesrが指定されたパスにディレクトリを作成する権限を持っていない場合、このコードはSecurityExceptionをスローします。したがって、Webアプリケーションを実行している場合は、十分な特権を持つユーザーでアプリケーションサーバーを実行します。

public String addUser(int useId, String name, String lastName, String country, String birthplace, String adress, String password) throws SQLException { 
    List<User> list = listAllUsers(); 
    int size = list.size(); 

    Connection conn = null; 
    PreparedStatement ps = null; 
    ResultSet rs = null; 

    for (int i = 0; i < size; i++) { 
     if (useId == list.get(i).getIdUser()) { 
      return "error"; 
     } 
    } 

    conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/testBaza?user=root"); 
    ps = conn.prepareStatement("INSERT INTO User (id_user,name,last_name,birthplace,country,adress,user_pass) values(?,?,?,?,?,?,?)"); 
    conn.setAutoCommit(false); 

    ps.setInt(1, useId); 
    ps.setString(2, name); 
    ps.setString(3, lastName); 
    ps.setString(4, country); 
    ps.setString(5, birthplace); 
    ps.setString(6, adress); 
    ps.setString(7, password); 

    ps.executeUpdate(); 
    conn.commit(); 
    conn.close(); 

    String folderName = "~/MyDocuments/" + id_user; // the folder name is user_id, you can use something else as well 
    new File(folderName).mkdir(); // the directory is created 

    return "userRegPage"; 
} 
+0

私は少なくとも私のディレクトリが作成されている場所を把握できません。Linuxファイルのパスは今私にとって混乱しています。 – Stanimir

0

私は

をそれを考え出しはちょうど私がファイルパスに自分のユーザー名を追加するために必要なように私のメソッドにこのコードが見えました。

関連する問題