Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. package javaapplication2;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. /**
  10. *
  11. * @author Maister
  12. */
  13. public class RegistroBiblio {
  14.  
  15. Connection cn;
  16. Statement st;
  17. ResultSet rs;
  18. String con, nombre="jose", dia="lunes 28";
  19.  
  20.  
  21. public void conectar(){
  22. try {
  23. Class.forName("com.mysql.jdbc.Driver");
  24. cn=DriverManager.getConnection("jdbc:mysql://localhost/biblioteca?user=root&password=");
  25. System.out.println("Conexion");
  26. } catch (Exception e) {
  27. System.out.println(e.toString());
  28. e.printStackTrace();
  29. }
  30.  
  31. }
  32. public void cerrar() throws SQLException{
  33. cn.close();
  34. System.out.println("Cerrar");
  35. }
  36. public void insertar(String nombre, String dia){
  37. try {
  38. conectar();
  39. con="insert into tabla (nombre, dia) values ('"+nombre+"', '"+dia+"')";
  40. st.execute(con);
  41. System.out.println("Insertar good");
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45.  
  46. }
  47. public void insertar2(String nombre, String dia){
  48. try {
  49. conectar();
  50. con="insert into p (uno, dos) values ('"+nombre+"', '"+dia+"')";
  51. st.execute(con);
  52. System.out.println("Insertar good");
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56.  
  57. }
  58. public void select(String nombre){
  59. try {
  60. conectar();
  61. con="select * from tabla where nombre='"+nombre+"'";
  62. rs=st.executeQuery(con);
  63. while(rs.next()){
  64. nombre=rs.getString("nombre");
  65. dia=rs.getString("dia");
  66. System.out.println("Select");
  67. }
  68.  
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. public static void main(String[] args) {
  74. // TODO code application logic here
  75. RegistroBiblio a=new RegistroBiblio();
  76. a.insertar("Jose", "Jueves");
  77. }
  78.  
  79. }
  80.  
  81. st.execute(con); // st debe estar a null
  82.  
  83. public static void main(String[] args) {
  84. // TODO code application logic here
  85. RegistroBiblio a=new RegistroBiblio();
  86. a.insertar("Jose", "Jueves");
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement