My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.lineadecodigo.java.jdbc;

/**
* @file ActualizarDatos.java
* @version 1.0
* @author Linea de Codigo (http://lineadecodigo.com)
* @date 27-marzo-2011
* @url http://lineadecodigo.com/java/actualizar-datos-con-jdbc/
* @description Utilización de JDBC y una sentencia UPDATE para actualizar los datos de una base de datos.
*/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ActualizarDatos {

public static void main(String[] args) {
Connection con = null;
PreparedStatement stmt = null;

// Definimos el driver y la url
String sDriver = "com.mysql.jdbc.Driver";
String sURL = "jdbc:mysql://localhost:3306/lineadecodigo";

// Cargamos el driver y realizamos la conexión
try{
Class.forName(sDriver).newInstance();
con = DriverManager.getConnection(sURL,"root","");

stmt = con.prepareStatement("UPDATE paises SET pais=? WHERE codigo=?");
stmt.setString(1,"España");
stmt.setString(2,"ES");

int retorno = stmt.executeUpdate();

if (retorno>0) {
System.out.println("Actualización ejecutada correctamente");
}



} catch (SQLException sqle){
System.out.println("SQLState: "
+ sqle.getSQLState());
System.out.println("SQLErrorCode: "
+ sqle.getErrorCode());
sqle.printStackTrace();

} catch (Exception e){
e.printStackTrace();
} finally {
if (con != null) {
try{
stmt.close();
con.close();
} catch(Exception e){
e.printStackTrace();
}
}
}

}

}

Change log

r356 by vcuervo on Mar 26, 2011   Diff
JDBC
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1751 bytes, 66 lines
Powered by Google Project Hosting