Advertisement
Guest User

Untitled

a guest
May 30th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.sql.*;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Properties;
  6.  
  7. public class ProfilesDAOTest {
  8.    
  9.  
  10.     public ProfilesDAOTest() throws Exception{
  11.         Connection con = null;
  12.         try {
  13.             Class.forName("com.mysql.jdbc.Driver");
  14.             Properties prop = new Properties();
  15.             prop.load(new FileInputStream("info.properties"));
  16.             String user = prop.getProperty("user");
  17.             String password = prop.getProperty("password");
  18.             String dburl = prop.getProperty("dburl");
  19.             con = DriverManager.getConnection(dburl, user, password);
  20.         } catch (ClassNotFoundException e) {
  21.             e.printStackTrace();
  22.         } catch (SQLException e) {
  23.             e.printStackTrace();
  24.         }
  25.         return ;
  26.     }
  27.     public void updateProfiles (HashMap<String,Integer> theProfiles) throws DAOException{
  28.         PreparedStatement myStmt = null;
  29.         Connection myConn = null;
  30.         String sql = "UPDATE profiles SET user_name=?, nick_name=?, user_mail=?" +
  31.                 ", password=?, gender=?, country=?";
  32.         try {
  33.             myConn.setAutoCommit(false);
  34.             myStmt = myConn.prepareStatement(sql);
  35.  
  36.             for (Map.Entry<String,Integer> e : theProfiles.entrySet()) {
  37.  
  38.                 myStmt.setString(1, String.valueOf(e.getValue().intValue()));
  39.                 myStmt.setString(2, String.valueOf(e.getValue().intValue()));
  40.                 myStmt.setString(3, String.valueOf(e.getValue().intValue()));
  41.                 myStmt.setString(4, String.valueOf(e.getValue().intValue()));
  42.                 myStmt.setString(5, String.valueOf(e.getValue().intValue()));
  43.                 myStmt.setString(6, String.valueOf(e.getValue().intValue()));
  44.                 myStmt.executeUpdate();
  45.                 myConn.commit();
  46.             }
  47.  
  48.         } catch (SQLException e) {
  49.             e.printStackTrace();
  50.         } finally {
  51.             try{
  52.                 if (myConn != null) {
  53.                 myConn.close();
  54.                 }
  55.                 if (myStmt != null) {
  56.                 myStmt.close();
  57.             }
  58.             } catch (SQLException e) {
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement