MysqlExample.java

Un sencillo ejemplo de como hacer un select a una base de datos mysql desde java

   1 import java.sql.*;
   2 
   3 public class MysqlExample {
   4     public static void main(String[] args) {
   5         try {
   6             Class.forName("com.mysql.jdbc.Driver").newInstance();
   7 
   8             Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/labasededatos?" +
   9                                     "user=elusuario&password=elpassword");
  10 
  11             Statement stmt = null;
  12             ResultSet rs = null;
  13 
  14             stmt = conn.createStatement();
  15             rs = stmt.executeQuery("SELECT celda FROM tabla");
  16 
  17             while(rs.next()){
  18                 System.out.println("celda: " + rs.getString("celda"));
  19             }
  20 
  21             rs.close();
  22             stmt.close();
  23         } catch (Exception ex) {
  24             System.out.println("Exception: " + ex);
  25         }
  26     }
  27 }

Compilacion

javac MysqlExample.java

Ejecucion

Enlaces


CategoryJava | CategoryProgramacion

Java/Mysql (last edited 2010-10-12 16:17:28 by Kmilo)