Home Answers Viewqa SQL insert and retrive data and time

 
 


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

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 Pages:
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
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
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
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 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
url parameter using retrive data from database in jsp
url parameter using retrive data from database in jsp  The user can... : Start Time : End Date : End Time : Time Zone : Status
insert
insert  insert data in database from servlet through JDBC   Ho Friend, Please visit the following: Insert data into database Thanks
java code to insert select at run time....????
java code to insert select at run time....????  java database code to retrieve data at runtime and please give codes for insert delete too.... using jsp and withot jsp   1)application.jsp: <%@ page import="java.sql.
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
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
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
how to retrive other details with an image - Swing AWT
how to retrive other details with an image  hello I m trying to retrive an image as well as some other values from database.. but only image... time from database? button2.addActionListener(new ActionListener
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 values from an arraylist and display in a jsp?
Retrive values from an arraylist and display in a jsp?  Hi, I need...); stmt.executeUpdate("insert into Students(name,year) values('"+name+"', '"+year+"')"); Close.... It might be helpful for you. http://www.roseindia.net/jsp/servlet-jsp-data
Retrive values from an arraylist and display in a jsp?
Retrive values from an arraylist and display in a jsp?  Hi, I need...); stmt.executeUpdate("insert into Students(name,year) values('"+name+"', '"+year+"')"); Close...://www.roseindia.net/jsp/servlet-jsp-data-list.shtml
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
insert data into mysql databse using swing
insert data into mysql databse using swing   Blockquote hi, here is my code, i want code for store data into mysql database when click on submit button from access data from this form, please provide code, import java.awt.
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
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
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
To Upload and insert the file into Database with Current Date and Time In JSP
Date and Time.  (ii). Prepared Statement is used to insert the data... a Query "INSERT INTO file_tbl set file_data='"+value+"',file_date='"+strDateNew+"'" to insert the data into database. Step 4: Output when
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"%>
how to insert the bulk data into the data base from the table of jsp page to another jsp page
to insert the marks details into the data base i have retrive the rollno and name and i have to insert the mark for n number student in the table i don't how...how to insert the bulk data into the data base from the table of jsp page
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
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
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
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
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
By dropdownlist retrive data from database and show in textbox in jsp.
By dropdownlist retrive data from database and show in textbox in jsp.  Hello Sir, I am doing project in jsp.I have to retrive data from database, when I select value from dropdownlist, and that data I want to show in textbox
retrive the data from access database to drop down list box in jsp
retrive the data from access database to drop down list box in jsp  hai, im new to jsp now im using the jsp along with access database.in table i load all the data's i need to retrive the data from database to dropdown list box
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
EJB Insert data
.style1 { color: #FFFFFF; } EJB Insert data... describes you the way  to insert data into the database using EJB. The steps involved in inserting data are as :- 1)Create an interface named
Insert Data From File
Insert Data From File       Insert Data From File is  used to insert data from file. Understand with Example The Tutorial illustrate an example to insert data from
url parameter using retrive data from database in jsp
url parameter using retrive data from database in jsp   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
after entering details in reg page,n enter the submit button,how can v store the data in db, n hw can v retrive the data frm db in jsp
the data in db, n hw can v retrive the data frm db in jsp  i ve creted... the submit button,how can v store the data in db, n hw can v retrive the data frm...,thr will b data na ,so hw to retrive that data.... rpy asap plzzz,i ve
To insert maximum number of data in database.
To insert maximum number of data in database.  how to change string data type into clob data type in jsp   Hi Friend, Please visit the following link: http://www.roseindia.net/jdbc/insert-clob-example.shtml Thanks
Date & TIme - Development process
Date & TIme  Hi, I asked to insert Date and Time to database with am... with time. Hi Friend, To insert the date,use the following code in your...() that will insert date with time into database. So to use this method, you have
how to retrive the particular data from database in php with mysql?
how to retrive the particular data from database in php with mysql?  ..._db("samp", $con); $Name=$_POST['unames']; $data=mysql_query("SELECT * FROM tbl_sample where Name='$Name'"); /*$info=mysql_fetch_array($data);*/ while
FAILED TO INSERT DATA FROM FUNCTION();
FAILED TO INSERT DATA FROM FUNCTION();  HELLO, I HAVE A PROBLEM TO INSERT DATA FROM OUTPUT FROM FUNCTION()... I WANT TO STORE THE OUTPUT IN DATABASE, BUT FAILED TO INSERT... before this.... ......................... function

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.