Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE DATABASE IF NOT EXISTS `prueba`;
- USE `prueba`;
- DROP TABLE IF EXISTS `moments`;
- CREATE TABLE `moments` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `fechhour` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.Timestamp;
- import java.util.Calendar;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JSpinner;
- public class principal extends javax.swing.JFrame {
- Connection conSet;
- public principal() {
- initComponents();
- try {
- DriverManager.registerDriver(new com.mysql.jdbc.Driver());
- } catch (SQLException ex) {
- Logger.getLogger(principal.class.getName()).log(Level.SEVERE, null, ex);
- }
- String url = "jdbc:mysql://localhost/prueba";
- try{
- String url1 = System.getProperty("JDBC_URL");
- if (url1 != null)
- url = url1;
- }catch (Exception e){
- System.out.println("Seguridad ignorada");
- }
- try {
- conSet = DriverManager.getConnection(url, "root", "12345678");
- } catch (SQLException ex) {
- Logger.getLogger(principal.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @SuppressWarnings("unchecked")
- private void initComponents() {una ventana y un boton}
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- Timestamp hoy=getNow();
- Object[][] values=new Object[1][2];
- values[0][0]="fechhour";
- values[0][1]=hoy;
- System.out.println("la variable hoy es de la clase... "+hoy.getClass());
- try {
- insert("moments", values);
- } catch (SQLException ex) {
- Logger.getLogger(principal.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void main(String args[]) {
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new principal().setVisible(true);
- }
- });
- }
- private javax.swing.JButton jButton1;
- public void insert(String tableName, Object values[][]) throws SQLException{
- String cadCampos="(";
- String cadValues="(";
- for(int i=0; i<values.length; i++){
- cadCampos+=values[i][0];
- cadValues+="?";
- if(i!=values.length-1){
- cadCampos+=", ";
- cadValues+=", ";
- }
- }
- cadCampos+=")";
- cadValues+=")";
- PreparedStatement pstmt = conSet.prepareStatement ("insert into "+tableName+cadCampos+" values "+cadValues);
- for(int i=0; i<values.length; i++){
- System.out.println(values[i][0]+": "+values[i][1]);
- pstmt.setObject(i+1, values[i][1]);
- }
- System.out.println("insert into "+tableName+cadCampos+" values "+cadValues);
- pstmt.execute();
- pstmt = conSet.prepareStatement ("commit");
- pstmt.execute();
- pstmt.close();
- }
- public static java.sql.Timestamp getNow(){
- java.util.Calendar calendarHoy=Calendar.getInstance();
- java.util.Date hoy=calendarHoy.getTime();
- Timestamp hoySql=new Timestamp(hoy.getTime());
- System.out.println("Hoy es: "+hoySql);
- return hoySql;
- }
- }
Add Comment
Please, Sign In to add comment