Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa SQL insert and retrive data and time
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


keshab
insert and retrive data and time
2 Answer(s)      6 years and 2 months ago
Posted in : SQL



Advertisement
View Answers

August 27, 2008 at 11:56 AM


Hi friend,

First to create table in database

CREATE TABLE tablename (
`id` bigint(20) NOT NULL auto_increment,
current_date_time datetime default NULL,
PRIMARY KEY (`id`)
)

To insert current date and time in database :

import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.text.SimpleDateFormat;

public class insertDateTime{

public static void main(String args[]){

String url = "jdbc:mysql://localhost:3306/";;
String dbName = "database";
String driver = "com.mysql.jdbc.Driver";
String userName = "";
String password = "";

Statement stmt = null;
Connection con;
int val;
try{

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url+dbName,userName,password);
java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);


String current_date_time = sdf.format(now) ;

String queryString = "INSERT INTO tablename set current_date_time='"+current_dateTime+"'";

//out.println(queryString);

stmt=con.createStatement();


val = stmt.executeUpdate(queryString);


}
catch(Exception e)
{}
}




}


To show the date and time :

import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.text.SimpleDateFormat;


public class showDateTime {

public static void main(String args[]){


String url = "jdbc:mysql://localhost:3306/";;
String dbName = "database";
String driver = "com.mysql.jdbc.Driver";
String userName = "";
String password = "";



Statement stmt = null;
Connection con;
int val;
ResultSet rs;
try{

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url+dbName,userName,password);
java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);


String current_dateTime = sdf.format(now) ;

String queryString = "select * from tablename";



//out.println(queryString);

stmt=con.createStatement();


rs = stmt.executeQuery(queryString);

while(rs.next())
{
System.out.println(rs.getString("current_date_time"));

}


}
catch(Exception e)
{}
}
}

For read more information to visit :

http://www.roseindia.net/

Thanks



April 1, 2012 at 5:54 PM


Hello i tried the above insert code bt am getting the following error com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sysdate(date)value('2012-04-01 05:45:43')' at line 1

bt when inserted the same query in mysql its working fine.

plzz tell me how to resolve it.

herz my code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        // TODO Auto-generated method stub
        try
        {

            System.out.println("Hello");
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/test","root","sayali");
            PreparedStatement pst = null;

            java.util.Date now = new java.util.Date();

            String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);


            String current_date_time = sdf.format(now) ;

            String query = "INSERT INTO sysdate(date)value(?)";
            pst = connection.prepareStatement(query);
            pst.setString(1,current_date_time);
            System.out.println(pst);

        //out.println(queryString);

            int UpdateQuery = pst.executeUpdate();
            System.out.println(UpdateQuery);


        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("error");
        }
    }


Related Tutorials/Questions & Answers:
insert and retrive data and time - SQL
insert and retrive data and time  hello everybody . Is there any method in java so that we can insert current system time and then it can... default NULL, PRIMARY KEY (`id`) ) To insert current date and time
how to insert data into database using jsp & retrive
how to insert data into database using jsp & retrive  Hello, I have created 1 html page which contain username, password & submit button. in my oracle10G database already contain table name admin which has name, password
Advertisements
can i use trigger to insert data within a time period ?
can i use trigger to insert data within a time period ?  Hello sir, i want to calculate company's closing account each Saturday 12.00. In this case i... this trigger concept the data will be insert to each Employee's ID. Plz sir help me
retrive data from database?
retrive data from database?  hellow i have a database sheet name..... now i want retrive sn,roll no and name and textbox like....,rllno,and name retrive
retrive data from database
retrive data from database   hi.. i made a application form. it's have attribute s.no,name,roll no and i enter a few records. now i want to view all record not in database access sheet i want to view it at any another
retrive record from the text field and insert into to database
retrive record from the text field and insert into to database  the following code is inserting values in the my sql database but i want to insert the data into oracle database , i want what changes i have to make
insert and retrive image from sql server database in jsp
insert and retrive image from sql server database in jsp  what is the code to insert and retrive an image from sql server database in jsp
To retrive data from database - Struts
To retrive data from database  How to get values ,when i select a select box in jsp and has to get values in textbox automatically from database? eg:I have three tables in database,and in jsp page if i select courseid in select
GWT -- retrive the data from Database
GWT -- retrive the data from Database  the user can create an event. In the create page, we have three buttons. Save as draft, preview and save and post buttons. Once the user entered all the datas and if he clicks the Preview
retrive data from oracle to jsp
retrive data from oracle to jsp  i am a beginer in jsp so please help... cost and manager name and storing it in the data base. in search proj fiel... connected to data base
insert data
insert data  i've got a problem to insert my data to database.i can...("insert into images values(?,?,?,?,?)"); st.setString(2... and as well as the passed data from mulitpart/form-data is greater than or equal to 0
Retrive the data from the table in data base using jdbc
Retrive the data from the table in data base using jdbc  Retrive the data from the table in data base using jdbc   JDBC Tutorials
how to retrive data grom database in jsp pages.
how to retrive data grom database in jsp pages.  sir, i have problem to retrieve data from table of the database on the html web pages , so would... immediately. i want sample code of jsp to retrieving data from table of database to html
Request URl using Retrive data from dtabase
Request URl using Retrive data from dtabase  Using With GWT the user can create an event. In the create page, we have three buttons. Save as draft, preview and save and post buttons. Once the user entered all the datas and if he
insert and delete data in database
insert and delete data in database  insert and delete data in database from servlets through JDBC   Hi Friend, Please visit the following links: Insert Data Delete Data Thanks
Oracle Database - Insert System Time
Oracle Database - Insert System Time  Hi I want to insert only system time dynamically using a statement but not a prepared statement into Oracle Database please suggest me any code
single query to insert the data
single query to insert the data  How to insert data for all HTML fields in single MYSQL query
correct the sql error and retrive data....plez
correct the sql error and retrive data....plez  i am getting a SQL Error while retriving data from access to jframe called "datatype mismatch in criteria expression" plez do help me and studentId's datatye is number
retrive data from database using jsp in struts?
retrive data from database using jsp in struts?   *search.jsp* <%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
retrive data from data base and print it in every page until logout
retrive data from data base and print it in every page until logout  suppose that a user login your website through user name & password. now after completion of login he goto home page.inside home page i want to print
insert data into database
insert data into database  hi,thanks for reply just i am doing student information project,frontend is jsp-servlet and backend is msaccess2007. i... the data into the jsp page that data stored into the database.Here the error
Insert Data From File
Insert Data From File       Insert Data From File is  used to insert data from file... to insert data from file. To understand this example, we create a table 'Stu_Table
how retrive data from google analystics dashbord on the client machine
how retrive data from google analystics dashbord on the client machine   sir i am digvijay subj: retrive data from google analystics dashbord respected sir, According above subject i request you , i want retrive data from
How to insert clob data??
How to insert clob data??  Can any one tell me in details how... to insert more than 4000 characters.I have heard that clob fields can hold 4 gb of data and need pl/sql to avail that feature.Can any one tell me how to create
database is connected but not insert the data
database is connected but not insert the data  hi, i am getting connected to database.Retrive the data also but cannot insert the data into database...=con.prepareStatement("insert into studentmaster(slno) values('"+batch+"')"); i
Retrive data from databse to a text field - Java Server Faces Questions
Retrive data from databse to a text field  Hi, Can you give me a source code to rertrve data from Mysql databse to a text field using JSF.....by selecting option from a dropdown option
insert data into database
insert data into database  type Exception report message...)Go to the start->Control Panel->Administrative Tools-> data sources. 2... selecting the driver, click finish button. 4)Then give Data Source Name
Advertisements
 

 

 

DMCA.com