A Simple Java Program to Insert Value in DataBase

import java.util.Date;
import java.sql.*;

public class DemoDate
{
public static void main(String args[])throws Exception
{
Date d=new Date();
long l=d.getTime();
System.out.println(l);
java.sql.Date obj=new java.sql.Date(l);
Class.forName("com.mysql.jdbc.Driver");  //type 4 driver
 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/college","root","rat");//connection
System.out.println("Connection successful:"+con);
 PreparedStatement ps=con.prepareStatement("insert into demo values(?)");
 ps.setDate(1, obj);
 ps.executeUpdate();
 System.out.println("operation successful");


 ps=con.prepareStatement("select * from demo");
 ResultSet res=ps.executeQuery();
 res.next();
 d=res.getDate(1);
 System.out.println(d);
}
}

2 comments: