Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class ConnJDBC {
- final static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- final static String DB_URL = "jdbc:mysql://localhost/DRAFT";
- final static String USER = "user";
- final static String PASS = "pass";
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- try {
- Class.forName(JDBC_DRIVER); // registrar driver JDBC
- conn = DriverManager.getConnection(DB_URL, USER, PASS); // abrir la conexion
- stmt = conn.createStatement();
- String sql = "SELECT nombre FROM empleados";
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- String nombre = rs.getString("nombre");
- System.out.println(nombre);
- }
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException e) { // errores JDBC
- e.printStackTrace();
- } catch (Exception e) { // otros errores, como de Class.forName
- e.printStackTrace();
- } finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException e) {
- // aca no se puede hacer nada
- }
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement