Tema: javasql
Ver Mensaje Individual
  #2 (permalink)  
Antiguo 21/07/2011, 15:21
DonVidela
 
Fecha de Ingreso: mayo-2011
Ubicación: Santiago
Mensajes: 20
Antigüedad: 13 años
Puntos: 2
Respuesta: javasql

Código java:
Ver original
  1. /**
  2. *
  3. * @author Soma
  4. *
  5.  
  6. *
  7. */
  8. package BD;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. public class BD {
  16. private String user;
  17. private String password;
  18. private String url;
  19. private String driverClassName;
  20. private Connection conn = null;
  21. private Statement stmt;
  22.  
  23. public BD(String user, String password, String url, String driverClassName) {
  24. this.user = user;
  25. this.password = password;
  26. this.url = url;
  27. this.driverClassName = driverClassName;
  28. }
  29. public BD()
  30. {
  31. this.user = "root";
  32. this.password="tupass";
  33. this.url = "jdbc:mysql://localhost/tu_basedatos";
  34. this.driverClassName = "com.mysql.jdbc.Driver"; // driver
  35. }
  36. public String getPassword() {
  37. return password;
  38. }
  39.  
  40. public String getUrl() {
  41. return url;
  42. }
  43.  
  44. public String getUser() {
  45. return user;
  46. }
  47.  
  48. public void setPassword(String password) {
  49. this.password = password;
  50. }
  51.  
  52. public void setUrl(String url) {
  53. this.url = url;
  54. }
  55.  
  56. public Connection getConn() {
  57. return conn;
  58. }
  59.  
  60. public void setConn(Connection conn) {
  61. this.conn = conn;
  62. }
  63.  
  64. public void setDriverClassName(String driverClassName) {
  65. this.driverClassName = driverClassName;
  66. }
  67.  
  68. public String getDriverClassName() {
  69. return driverClassName;
  70. }
  71.  
  72. public void setUser(String user) throws SQLException {
  73. this.user = user;
  74. }
  75.  
  76. public void conectar() throws SQLException {
  77. try {
  78. Class.forName(this.driverClassName).newInstance();
  79. this.conn = DriverManager.getConnection(this.url, this.user, this.password);
  80.  
  81. } catch (Exception err) {
  82. System.out.println("Error " + err.getMessage());
  83. }
  84. }
  85.  
  86. public ResultSet obtenerDatos(String sql) throws SQLException {
  87.  
  88. this.stmt = conn.createStatement();
  89. return this.stmt.executeQuery(sql);
  90. }
  91.  
  92. public void actualizar(String sql) throws SQLException {
  93. this.stmt = conn.createStatement();
  94. stmt.executeUpdate(sql);
  95. }
  96. public ResultSet ExeGet(String Q) throws SQLException{
  97. Statement st = this.conn.createStatement();
  98. return (ResultSet) st.executeQuery(Q);
  99. }
  100. public int Exe(String Q) throws SQLException{
  101. Statement st = this.conn.createStatement();
  102. return st.executeUpdate(Q);
  103. }
  104.  
  105. public void Off() throws SQLException{
  106. this.conn.close();
  107. }
  108.  
  109. }

Te recomiendo usar esa clase para llamar a conexion de base de datos aqui un ejemplo de como usarlo


Código java:
Ver original
  1. try{
  2.         con.conectar();
  3.        
  4.         //haciendo pruebas
  5.        
  6.      ResultSet datos = con.ExeGet("select * from persona where rut='"+form.getRut()+"';");
  7.     //sacamos todo del resulset
  8.      while(datos.next()){
  9.          //lo enviamos a un page input y lo imprimimos para verlo en la consola tambien
  10.             System.out.println("rut :"+ datos.getObject("rut"));
  11.             System.out.println("nombre :"+ datos.getObject("nombre"));
  12.             System.out.println("edad :"+ datos.getObject("edad"));
  13.             System.out.println("direccion : "+ datos.getObject("direccion"));
  14.             //////////////////outposts
  15.             f.addActionOutput("rut", datos.getObject("rut"));
  16.             f.addActionOutput("nombre", datos.getObject("nombre"));
  17.             f.addActionOutput("edad", datos.getObject("edad"));
  18.             f.addActionOutput("direccion", datos.getObject("direccion"));
  19.          
  20.      }
  21.     con.Off();

System.out.println("rut :"+ datos.getObject("rut")); // aqui sacas el rut del campo llamado rut... si tienes dudas pregunta