Advertisement
0no

Inserção no banco de dados

0no
Oct 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. //Inserção no banco de dados
  2.  
  3. Conexao con = Conexao.getInstance();   
  4. String sql = "SELECT * FROM TABELA WHERE USUARIO = '10'";
  5. PreparedStatement STATEMENT = con.getConnection().prepareStatement(sql);
  6. ResultSet rs = STATEMENT.executeQuery();
  7.     if(rs.isBeforeFirst()){ //Verifica se existem registros no ResultSet
  8.         rs.next();
  9.         String sql = "INSERT INTO TABELA(CAMPO, CAMPO) VALUES(?,?)";
  10.         PreparedStatement ps = con.getConnection().prepareStatement(sql);
  11.         ps.setString(1, rs.getString("CAMPO"));
  12.         ps.setString(2, rs.getInt("CAMPO"));
  13.         int i = ps.executeUpdate();
  14.         if(i == 1) //Verifica se o registro foi inserido
  15.         {
  16.             System.out.println("Inserido com sucesso!");
  17.         }else{
  18.             System.out.println("Problemas na inserção dos dados!");
  19.         }
  20.     }else {
  21.         System.out.println("Problemas na inserção dos dados!");
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement