Mysql database Modify method to add date format columns _mysql

Source: Internet
Author: User
Import java.sql.*;
Import Java.text.DateFormat;  
     Database query public class Selecttable {String dbdriver= "com.mysql.jdbc.Driver"; 
     String dburl= "Jdbc:mysql://localhost:3306/sss"; Change string username= "root" according to the actual situation; 
     String password= "123"; 
       Public Connection Getconn () {Connection conn=null; 
       try {class.forname (dbdriver); 
       catch (ClassNotFoundException e) {e.printstacktrace (); 
       try {conn = drivermanager.getconnection (Dburl,username,password);//Note three parameters} 
       catch (SQLException e) {e.printstacktrace (); 
     Return conn;
        public void Select () {Connection conn = Getconn (); try{Statement stmt = conn.createstatement ();//Create Statement Object System.out.println ("Successfully connected to the database!")
        ");  String sql = "SELECT * from JDBC"; SQL ResultSet rs = stmt.executequery (sql) to execute;//Create a 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 = Getconn (); try{Statement stmt = Conn.createstatement (); System.out.println ("Successfully connected to the database!")
");
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");//convert time of YYYY-MM-DD format to date long T = Dd.gettime ();
 

Java.sql.Date Date = new Java.sql.Date (t);
Pst.setint (1, 5);
Pst.setstring (2, "Limazhi");
Pst.setdate (3, date);
Pst.executeupdate ();
Select ();
 
}catch (Exception e) {e.printstacktrace ();} public static void Main (String args[]) {selecttable st = new Selecttable (); st.insERT (); }
     
}

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.