Mysql database modification to add Date Format columns, mysqldate

Source: Internet
Author: User
Tags dateformat

Mysql database modification to add Date Format columns, mysqldate

Import java. SQL. *; import java. text. dateFormat; // query public class SelectTable {String dbDriver = "com. mysql. jdbc. driver "; String dbUrl =" jdbc: mysql: // localhost: 3306/sss "; // String username =" root "; String password =" 123 "based on actual conditions "; public Connection getConn () {Connection conn = null; try {Class. forName (dbDriver);} catch (ClassNotFoundException e) {e. printStackTrace ();} try {conn = DriverManager. getC Onnection (dbUrl, username, password); // note that there are three parameters} catch (SQLException e) {e. printStackTrace () ;}return conn;} public void select () {Connection conn = getConn (); try {Statement stmt = conn. createStatement (); // create the Statement object System. out. println ("successfully connected to the database! "); String SQL =" select * from jdbc "; // The SQL ResultSet rs = stmt.exe cuteQuery (SQL) to be executed; // create the Data Object System. out. println ("id" + "\ t" + "name" + "\ t" + "brithday"); while (rs. next () {System. out. print (rs. getInt (1) + "\ t"); System. out. print (rs. getString (2) + "\ t"); System. out. print (rs. getDate (3) + "\ t"); System. out. println () ;}} catch (Exception e) {e. printStackTrace () ;}} public void insert () {Connection conn = getCo Nn (); try {Statement stmt = conn. createStatement (); System. out. println ("connected to the database successfully! "); String SQL =" insert into jdbc (id, name, birthday) values (?,?,?) "; PreparedStatement pst = conn. prepareStatement (SQL); DateFormat df = DateFormat. getDateInstance (); java. util. date dd = df. parse ("2000-12-12"); // converts the time in YYYY-MM-DD format to datelong t = dd. getTime (); java. SQL. date date = new java. SQL. date (t); pst. setInt (1, 5); pst. setString (2, "limazhi"); pst. setDate (3, date00000000pst.exe cuteUpdate (); select ();} catch (Exception e) {e. printStackTrace () ;}} public static void main (String args []) {SelectTable st = new SelectTable (); st. insert ();}}

How does mysql change the date format?

Check the current value through SQL statement Query

Show variables like '% date % ';
The default value is:
Date_format = % Y-% m-% d
Datetime_format = % Y-% m-% d % H: % I: % s

Add the following content to the mysql configuration file my. cnf or my. ini.
[Mysqld]
Date_format = % Y/% m/% d
Datetime_format = % Y/% m/% d % H: % I: % s
Finally, restart the mysql server.

For the date and time fields in the MySQL database

DateFormat dateFormat;
DateFormat = new SimpleDateFormat ("yyyy-MM-dd ");
String sToTimestamp = "14:21:12. 123 ";
Java. util. Date timeDate = dateFormat. parse (sToTimestamp );

In this case, convert a string to util. date. However, we recommend that you use TimeStamp when defining the time attribute.

Here, you can use the custom time string you added on the page.

Yes, you can simply format and write as needed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.