Last active
August 29, 2015 14:14
-
-
Save jorgeyp/dfa5174f34bbed37e971 to your computer and use it in GitHub Desktop.
MySQL Java connection example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String SQL_DRV = "com.mysql.jdbc.Driver"; | |
String SQL_URL = "jdbc:mysql://" | |
+ host + ":" + port | |
+ "/" + dbName | |
+ "?user=" + user | |
+ "&password=" + password; | |
Class.forName(SQL_DRV); | |
con = DriverManager.getConnection(SQL_URL); | |
PreparedStatement ps = null; | |
ResultSet rs; | |
Connection con = null; | |
try { | |
Class.forName(SQL_DRV); | |
con = DriverManager.getConnection(SQL_URL); | |
/* Ejemplo de lectura | |
ps = con.prepareStatement("SELECT max(id) AS max FROM Reservation;\n"); | |
rs = ps.executeQuery(); | |
while (rs.next()) { | |
id = rs.getInt("id"); | |
} | |
*/ | |
/* Ejemplo de inserción | |
ps = con.prepareStatement("INSERT INTO Reservation VALUES(?,?,?,?,?,?,?,?,?,now())"); // Ejemplo de inserción | |
ps.setInt(1, reservation.getId()); | |
ps.setInt(2, userId); | |
ps.setInt(3, reservation.getDepartureSchedule().getId()); | |
ps.setInt(4, reservation.getReturnSchedule().getId()); | |
ps.setInt(5, reservation.getSeat()); | |
ps.setBoolean(6, reservation.isBike()); | |
ps.setBoolean(7, reservation.isPet()); | |
ps.setBoolean(8, reservation.isInsurance()); | |
ps.setString(9, reservation.getCode()); | |
ps.executeUpdate(); | |
*/ | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw (e); | |
} finally { | |
try { | |
ps.close(); | |
con.close(); | |
} catch (Exception e) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment