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.