· 7 years ago · Jan 30, 2019, 09:30 PM
1CREATE DATABASE IF NOT EXISTS `prueba`;
2USE `prueba`;
3
4DROP TABLE IF EXISTS `moments`;
5
6CREATE TABLE `moments` (
7 `id` int(11) NOT NULL AUTO_INCREMENT,
8 `fechhour` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
9 PRIMARY KEY (`id`)
10) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
11
12import java.sql.Connection;
13import java.sql.DriverManager;
14import java.sql.PreparedStatement;
15import java.sql.SQLException;
16import java.sql.Timestamp;
17import java.util.Calendar;
18import java.util.logging.Level;
19import java.util.logging.Logger;
20import javax.swing.JSpinner;
21
22public class principal extends javax.swing.JFrame {
23
24 Connection conSet;
25
26 public principal() {
27 initComponents();
28
29 try {
30 DriverManager.registerDriver(new com.mysql.jdbc.Driver());
31 } catch (SQLException ex) {
32 Logger.getLogger(principal.class.getName()).log(Level.SEVERE, null, ex);
33 }
34 String url = "jdbc:mysql://localhost/prueba";
35 try{
36 String url1 = System.getProperty("JDBC_URL");
37 if (url1 != null)
38 url = url1;
39 }catch (Exception e){
40 System.out.println("Seguridad ignorada");
41 }
42 try {
43 conSet = DriverManager.getConnection(url, "root", "12345678");
44 } catch (SQLException ex) {
45 Logger.getLogger(principal.class.getName()).log(Level.SEVERE, null, ex);
46 }
47
48
49
50 }
51
52 @SuppressWarnings("unchecked")
53
54 private void initComponents() {una ventana y un boton}
55
56 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
57 Timestamp hoy=getNow();
58 Object[][] values=new Object[1][2];
59 values[0][0]="fechhour";
60 values[0][1]=hoy;
61 System.out.println("la variable hoy es de la clase... "+hoy.getClass());
62 try {
63 insert("moments", values);
64 } catch (SQLException ex) {
65 Logger.getLogger(principal.class.getName()).log(Level.SEVERE, null, ex);
66 }
67
68 }
69
70 public static void main(String args[]) {
71
72 try {
73 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
74 if ("Nimbus".equals(info.getName())) {
75 javax.swing.UIManager.setLookAndFeel(info.getClassName());
76 break;
77 }
78 }
79 } catch (ClassNotFoundException ex) {
80 java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
81 } catch (InstantiationException ex) {
82 java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
83 } catch (IllegalAccessException ex) {
84 java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
85 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
86 java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
87 }
88
89 java.awt.EventQueue.invokeLater(new Runnable() {
90 public void run() {
91 new principal().setVisible(true);
92 }
93 });
94 }
95
96 private javax.swing.JButton jButton1;
97
98 public void insert(String tableName, Object values[][]) throws SQLException{
99 String cadCampos="(";
100 String cadValues="(";
101 for(int i=0; i<values.length; i++){
102 cadCampos+=values[i][0];
103 cadValues+="?";
104 if(i!=values.length-1){
105 cadCampos+=", ";
106 cadValues+=", ";
107 }
108 }
109 cadCampos+=")";
110 cadValues+=")";
111 PreparedStatement pstmt = conSet.prepareStatement ("insert into "+tableName+cadCampos+" values "+cadValues);
112 for(int i=0; i<values.length; i++){
113 System.out.println(values[i][0]+": "+values[i][1]);
114 pstmt.setObject(i+1, values[i][1]);
115 }
116 System.out.println("insert into "+tableName+cadCampos+" values "+cadValues);
117 pstmt.execute();
118 pstmt = conSet.prepareStatement ("commit");
119 pstmt.execute();
120 pstmt.close();
121 }
122
123 public static java.sql.Timestamp getNow(){
124 java.util.Calendar calendarHoy=Calendar.getInstance();
125 java.util.Date hoy=calendarHoy.getTime();
126 Timestamp hoySql=new Timestamp(hoy.getTime());
127 System.out.println("Hoy es: "+hoySql);
128 return hoySql;
129 }
130
131}