Greetings,
I have the following code below which allows the date to be added via a JDBC connection as a STRING.
The value of dateString is inserted into the MS ACCESS database.
What is the easiest method to add the current date/time (dd/mm/yy hh:mm:ss) via JDBC in DATE format i.e. convert STRING to DATE or format the DATE without changing its type and insert it.
I'm a newbie to Java/Jsp too. -
<%@ page import="java.util.Date, java.text.SimpleDateFormat" %>
-
<% java.util.Locale locale = request.getLocale(); %>
-
-
<%
-
Date d = new Date();
-
String dateString = getFormattedDate (d);
-
%>
-
-
<%!
-
String getFormattedDate(Date d)
-
{
-
SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yy hh:mm:ss");
-
return simpleDate.format(d);
-
}
-
%>
-
19 20247 BigDaddyLH 1,216
Recognized Expert Top Contributor
I'm confused. What are you trying to do?
It sounds like you want to insert a row into a database table, with a Date value in one of its columns. - String sql = "INSERT INTO tablename(columnname1, columnname2) VALUES (?,?)";
-
PreparedStatement ps = con.prepareStatement(sql);
-
try {
-
ps.setDate(1, date);
-
ps.setString(2, someString);
-
ps.executeUpdate();
-
} finally {
-
ps.close();
-
}
Sorry, I will include the code for the SQL record insert which shows the Datestring value being passed in.
So to clarify I get the current date and time and insert into a record in my access database. - statement.executeUpdate("INSERT INTO FAQ (\"Id\",\"category\", \"question\", \"answer\", \"sequence\", \"UserId\", \"created\") VALUES ('"+IdParam+"','"+categoryParam+"','"+questionParam+"','"+answerParam+"', '"+sequence+"', '"+ session.getAttribute("theName")+"', '"+dateString+"') ");
-
Currently dateString is a string as in the database model and I want to convert the string into a date or have another method of inserting the current date (dd/mm/yy hh:mm:ss) into the database as a DATE rather than STRING.
Thanks
Rob
Thanks I will have a read of that, but at the moment my priority is to change my date in format "dd/mm/yy hh:mm:ss" into a java date format rather than a string.
Doesnt look professional have a date field in my database as TEXT :(
BigDaddyLH 1,216
Recognized Expert Top Contributor
What is the source of this dateString? I'm suggesting that if this string is being generated from a Date object, don't! Keep it a Date object and you won't have to parse it.
Thanks for the reply.
Its starts of as a date see code below (JSP code) - <%
-
Date d = new Date();
-
String dateString = getFormattedDate (d);
-
%>
Because I dont know any better :-( I change it to a string so I can manipulate it, see below:- -
<%!
-
String getFormattedDate(Date d)
-
{
-
SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yy hh:mm:ss");
-
return simpleDate.format(d);
-
}
-
%>
As long as I can get a date format of dd/MM/yy hh:mm:ss as a DATE I'll be happy!
BigDaddyLH 1,216
Recognized Expert Top Contributor
You want a date string so that you can turn it back into a Date object???
Why isn't that good enough?
Current date goes into the MS Access database as a string as its setup as Text on the database.
I've just changed the database schema to date/time and used the value of the date i.e
I need to check about the SQL inserts as It' really confusing me as it seem all values require a single quote and a double i.e. '" no matter if string or integer ??? - newstatement.executeUpdate("INSERT INTO FAQ (\"category\", \"question\", \"answer\", \"sequence\", \"UserId\", \"created\") VALUES ('"+categoryParam+"','"+questionParam+"','"+answerParam+"', '"+sequence+"', '"+ session.getAttribute("theName")+"', '"+d+"' )");
Using the code above it complains of data type mismatch, I will have to find out if i can insert dates in this way using SQL.
Cheers
Rob
BigDaddyLH 1,216
Recognized Expert Top Contributor
Trust me, I've said this more than once in this thread: you never want to write code like what you just posted. Use a PreparedStateme nt.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: JShurmatz |
last post by:
If anyone can shed some light on this problem I would greatly
appreciate it.
I am unsuccessfully trying to use a database connnection retrieved
from a pool configured using Java System Web Server 6.1 with the SQL
Server 2000 JDBC Driver.
The background:
1. I have downloaded the SQL Server 2000 JDBC Driver and installed it
on the web server.
|
by: CM |
last post by:
Hi,
when i want connect me in my BD with a JSP (with this simple code),
this exception is throw.
Thank's for ur help
Mathieu
CODE of my JSP
---------------------
|
by: dinesh prasad |
last post by:
I'm trying to use a servlet to process a form, then send that data to
an SQL server stored procedure. I'm using the WebLogic 8 App. server.
I am able to retrieve database information, so I know my application
server can talk to the database.
I've determined the failure occurs when the the following statement is
executed: cstmt.execute(); (due to the failure of println statements
placed afterwards). I get the following error after trying to...
|
by: Hassan Naqvi |
last post by:
Hi,
Basically, I am Java developer. In past I have played with Oracle
using Java (JDBC). But this is the time to play with IBM DB2 using
Java (JDBC). So kindly help this DB2 newbie.
I have a file of 32 MB, named as "fdbk5.0". This is actually a
database created in DB2 and containing records. I am interested in
viewing the *structure of this database, i.e. table names, column
names and finally all the records. How I can do this? Which...
|
by: Jobs |
last post by:
Download the JAVA , .NET and SQL Server interview with answers
Download the JAVA , .NET and SQL Server interview sheet and rate
yourself. This will help you judge yourself are you really worth of
attending interviews. If you own a company best way to judge if the
candidate is worth of it.
http://www.questpond.com/InterviewRatingSheet.zip
| |
by: mjahabarsadiq |
last post by:
HELLO FRIENDS
I HAVE ONE JAVA CODE WHICH PARSES AN XML FILE AND PRODUCE A QUERY TO CREATE TABEL IN A DATABASE.
BUT I NEED THE CODE TO BE USED IN A JSP PAGE.
HOW TO USE THIS PAGE WITH JSP.
I AM USING TOMCAT 4.1 WEBSERVER FOR COMPILING JSP PAGES.
|
by: vijaykumardahiya |
last post by:
Hello Sir,
I have a simple Issue but It is not resolve by me i.e input parameter
are not store in Ms-Access.
I store the input parameter through Standard Action <jsp:useBean>.
jsp:useBean call a property IssueData. this property exist in
SimpleBean which create a connection from DB and insert the data.
At run time servlet and server also show that loggging are saved in DB.
But when I open the table in Access. Its empty.
Ms-Access have...
|
by: asti1987 |
last post by:
I have a big problem, y need to print a jasper report, but it must to be with jsp.
this is my jsp
please help me
<%@ page import="java.io.*,java.util.*,java.net.*,java.sql.*,imprimir.imprimir" %>
<%
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |