简体   繁体   中英

How do you add a JAVA TimeStamp for my SQL to work?

I have to make a Refuge program which saves the information on a database; I'm having problem with the date_entry Variable since I'm not sure how to implement it. The database says that the entry for this should be in TimeStamp, but im not sure how to make it work on my code; tried making a function and then assign the function to my variable but yeah, didn't work. I added the database just in case. This is what i got until now:

JButton btnInsert = new JButton("Insert");
        btnInsert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String name = txtPetName.getText();
                String photo = txtPetPhoto.getText();
                String type = txtPetType.getText();
                String color = txtColor.getText();
                int gender = Integer.parseInt(txtGender.getText());
                int isSterilized = Integer.parseInt(txtSterlized.getText());
                String pedigree = txtPedigree.getText();
                String dateEntry =  showDate();         //HERE'S MY DOUBT>
                String vaccine1 = txtVaccine1.getText();
                String vaccine2 = txtVaccine2.getText();
                String vaccine3 = txtVaccine3.getText();

                Connection conn = null;
                PreparedStatement pstmt = null;

                try {

                    Class.forName("com.mysql.jdbc.Driver");
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/refugio","root","2797");
                    pstmt = conn.prepareStatement("INSERT INTO PETS VALUES(?,?,?,?,?,?,?,?,?,?)");
                    pstmt.setString(1, name);
                    pstmt.setString(2, photo);
                    pstmt.setString(3, type);
                    pstmt.setString(4, color);
                    pstmt.setInt(5, gender);
                    pstmt.setInt(6, isSterilized);
                    pstmt.setString(7, pedigree);
                    pstmt.setString(8, showDate()); //THEN HERE
                    pstmt.setString(9, vaccine1);
                    pstmt.setString(10, vaccine2);
                    pstmt.setString(11, vaccine3);
                    int i = pstmt.executeUpdate();

                    if(i>0) {
                        JOptionPane.showMessageDialog(null, "Data was saved");
                    }else {
                        JOptionPane.showMessageDialog(null, "Data was not saved");
                    }


                }catch(Exception ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
        });
        btnInsert.setBounds(29, 715, 379, 54);
        contentPane.add(btnInsert);
/**
DATABASE:
CREATE DATABASE refugio;

USE refugio;

CREATE TABLE Pets
(
   petName VARCHAR(15) PRIMARY KEY NOT NULL,
   petPhoto VARCHAR(50) NULL,
   petType VARCHAR(10) NOT NULL,
   color VARCHAR(15),
   gender TINYINT(1) NOT NULL COMMENT '0 - female, 1 - male',
   isSterilized TINYINT(1) COMMENT '0 - NO, 1 - YES',
   pedigree VARCHAR(15),
   date_entry timestamp NOT NULL,
   vaccine1 VARCHAR(15) NULL,
   vaccine2 VARCHAR(15) NULL,
   vaccine3 VARCHAR(15) NULL
);
*/
import java.sql.Timestamp;
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);

credits: here

The TIMESTAMP type in MySQL is akin to the standard SQL type TIMESTAMP WITH TIME ZONE .

The appropriate Java class is OffsetDateTime .

Capture the current moment as seen in a time zone.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Extract an OffsetDateTime .

OffsetDateTime odt = zdt.toOffsetDateTime() ;

Send to the database.

myPreparedStatement.setObject( … , odt ) ;

Retrieval.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

This has been covered many times already on Stack Overflow. So search to learn more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM