【问题标题】:How to insert Time ( HH:MM:SS) into MySQL Database table in Java? [duplicate]如何在 Java 中将时间 (HH:MM:SS) 插入 MySQL 数据库表中? [复制]
【发布时间】:2018-01-21 06:28:27
【问题描述】:

我有一个列 TIME 类型的表(名为 myTime)。 字符串 t="15:50:00"; 如何将此字符串转换并插入到myTime 列(HH:MM:SS)。

谢谢!

【问题讨论】:

  • 您甚至可以在mysql表中使用“varchar”作为数据类型并将其直接存储为字符串。稍后,如果需要,您可以通过 Java 代码获取字符串并转换为 Date 对象
  • 拜托了!我需要 TIME 类型而不是 DATETIME
  • 欢迎来到 Stack Overflow。请在发布之前研究您的问题。在许多情况下,您会通过搜索引擎更快地找到答案。如果您不这样做,当您告诉我们您找到了什么和没有找到什么、您尝试了什么以及您仍然缺少什么时,我们可以更准确地为您提供指导。
  • 您是否使用PreparedStatement 插入数据库?还是 Hibernate、SQL*J 或其他一些奇特的机制?
  • 对于重复标记:要将时间作为 15:50:00 存储到数据库中,您应该更喜欢 not 使用 java.util.Date,如链接的问题及其接受的答案。最好使用java.time.LocalTime

标签: java mysql sql time localtime


【解决方案1】:

您可以使用String 数据类型来表示Time 值,也可以使用MySQL Time 数据类型并在您的Java 代码中使用preparedStatement.setTime(),例如:

你的桌子是:

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

您的 Java 代码可能如下所示:

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();
      }
   }
}

【讨论】:

  • 请不要教年轻人使用早已过时且臭名昭著的课程DateTimeSimpleDateFormat。至少不是第一选择,也没有任何保留。今天我们做得更好了。
【解决方案2】:

您可以使用 TIME 数据类型。 例如,

CREATE TABLE tests (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(500) NOT NULL,
    start_time TIME,
    end_time TIME
);

【讨论】:

  • 我有一个表,其中有一列(名为 myTime)TIME 类型。但我无法将字符串 t = string t ="15:50:00" 插入此列。如何将此字符串转换并插入到 myTime 列中。请!
  • SQL 数据类型请使用setString()
【解决方案3】:

您可以使用setString() 设置任何SQL 数据类型。试试这样的:

prepStatement.setString("myTime", "15:50:00");

【讨论】:

    【解决方案4】:

    我自己没有经验,但你能做的最好的是将时间保留在 Java 中的 LocalTime 对象中,并使用 yourPreparedStatement.setObject(parameterIndex, yourTime); 将时间设置为 SQL insert 中的值或update 声明。我相信您可以在那里找到代码示例、教程、文档等。请去搜索。

    那么你从哪里得到LocalTime 对象呢?

    LocalTime yourTime = LocalTime.parse(t);
    

    (其中t 是您的时间字符串,例如问题中的15:50:00

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 2012-05-22
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      相关资源
      最近更新 更多