⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timetest.java

📁 MySql Java Connector
💻 JAVA
字号:
/* Copyright (C) 2002-2004 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation.  There are special exceptions to the terms and conditions of the GPL  as it is applied to this software. View the full text of the  exception exception in file EXCEPTIONS-CONNECTOR-J in the directory of this  software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */package testsuite.simple;import testsuite.BaseTestCase;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.Time;import java.sql.Timestamp;import java.util.Properties;import java.util.TimeZone;/** * Tests time functionality. * * @author Mark Matthews * @version TimeTest.java,v 1.2 2002/12/17 21:41:02 mmatthew Exp */public class TimeTest extends BaseTestCase {    /**     * Constructor for TimeTest.     *     * @param name the test to run     */    public TimeTest(String name) {        super(name);    }    /**     * Runs all test cases in this test suite     *     * @param args     */    public static void main(String[] args) {        junit.textui.TestRunner.run(TimeTest.class);    }    /**     * Tests timezone-related functionality     *     * @throws Exception if an error occurs     */    public void testTimezone() throws Exception {        try {            String clientTimezoneName = "America/Los_Angeles";            TimeZone.setDefault(TimeZone.getTimeZone(clientTimezoneName));            Properties props = new Properties();            props.put("useTimezone", "true");            conn = DriverManager.getConnection(dbUrl, props);            stmt = conn.createStatement();            stmt.executeUpdate("DROP TABLE IF EXISTS timeTest");            stmt.executeUpdate(                "CREATE TABLE timeTest (tstamp DATETIME, t TIME)");            PreparedStatement pstmt = conn.prepareStatement(                    "INSERT INTO timeTest VALUES (?, ?)");            long now = System.currentTimeMillis(); // Time in milliseconds since 1/1/1970 GMT            Timestamp nowTstamp = new Timestamp(now);            Time nowTime = new Time(now);            pstmt.setTimestamp(1, nowTstamp);            pstmt.setTime(2, nowTime);            pstmt.executeUpdate();            rs = stmt.executeQuery("SELECT * from timeTest");            while (rs.next()) {                String retrTimestampString = rs.getString(1);                Timestamp retrTimestamp = rs.getTimestamp(1);                rs.getTime(2);                System.out.println(retrTimestampString + " in database is "                    + retrTimestamp + " in " + clientTimezoneName);            }        } finally {            stmt.executeUpdate("DROP TABLE IF EXISTS timeTest");        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -