Search code examples
javamysqlsqltimelocaltime

How to insert Time ( HH:MM:SS) into MySQL Database table in Java?


I've a table with a column TIME type (named myTime). string t ="15:50:00"; How to convert and insert this string into myTime column (HH:MM:SS).

Thank you!


Solution

  • You can use String data type to represent the Time value, or you can use MySQL Time data type and in your Java code use preparedStatement.setTime(), for example:

    Your table is:

    CREATE my_table (
        id          INT          PRIMARY KEY AUTO_INCREMENT,
        name        VARCHAR2(30) NOT NULL,
        time_from   TIME
    );
    

    Your Java code can look like this:

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.Time;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class MySQLDatabaseDemo {
    
       Connection conn = null;
       PreparedStatement preparedStatement = null;
    
       public static Connection getConnection() throws Exception {
          String driver = "org.gjt.mm.mysql.Driver";
          String url = "jdbc:mysql://localhost/databaseName";
          String username = "root";
          String password = "root";
          Class.forName(driver);
          Connection conn = DriverManager.getConnection(url, username, 
                                                        password);
          return conn;
       }
    
       /**
        * @param args [0] = value of "id"
        *             [1] = value of "name"
        *             [2] = value of "time_from"
        */
       public void insertRowWithTimeDatatype(String[] args) {
    
          String query = "insert into my_table (id, name, timefrom) " + 
                                       "values (?, ?, ?)";      
    
          DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
          Date date = sdf.parse(args[2]);
          Time time = new Time(date.getTime());
    
          try {
             conn = getConnection();  // getConnection() is YOUR method
    
             preparedStatement = conn.prepareStatement(query);
    
             preparedStatement.setInt(1, Integer.parseInt(args[0]));
             preparedStatement.setString(2, args[1]);
             preparedStatement.setTime(3, time);
    
             // Execute statement and return the number of rows affected
             int rowCount = preparedStatement.executeUpdate();
             System.out.println("Number of rows affected: " + rowCount);
          } finally {
             preparedStatement.close();
             conn.close();
          }
       }
    }