473,799 Members | 3,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java dates for JDBC (JSP) String/Date conversions.

108 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.util.Date, java.text.SimpleDateFormat" %>
  2. <% java.util.Locale locale = request.getLocale(); %>    
  3.  
  4. <%
  5.     Date d = new Date();
  6.     String dateString = getFormattedDate (d);
  7. %>
  8.  
  9. <%! 
  10.     String getFormattedDate(Date d)
  11.    {
  12.     SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yy hh:mm:ss");
  13.     return simpleDate.format(d);
  14.    }
  15. %>
  16.  
Mar 13 '08 #1
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.

Expand|Select|Wrap|Line Numbers
  1. String sql = "INSERT INTO tablename(columnname1, columnname2) VALUES (?,?)";
  2. PreparedStatement ps = con.prepareStatement(sql);
  3. try {
  4.     ps.setDate(1, date);
  5.     ps.setString(2, someString);
  6.     ps.executeUpdate();
  7. } finally {
  8.     ps.close();
  9. }
Mar 13 '08 #2
robtyketto
108 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. statement.executeUpdate("INSERT INTO FAQ (\"Id\",\"category\", \"question\", \"answer\", \"sequence\", \"UserId\", \"created\") VALUES ('"+IdParam+"','"+categoryParam+"','"+questionParam+"','"+answerParam+"', '"+sequence+"', '"+ session.getAttribute("theName")+"', '"+dateString+"')   ");
  2.  
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
Mar 13 '08 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
Here's one of the 10 commandments of JDBC:

Don't use Statement, use PreparedStateme nt,

http://java.sun.com/docs/books/tutor.../prepared.html
Mar 14 '08 #4
robtyketto
108 New Member
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 :(
Mar 14 '08 #5
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.
Mar 14 '08 #6
robtyketto
108 New Member
Thanks for the reply.

Its starts of as a date see code below (JSP code)

Expand|Select|Wrap|Line Numbers
  1. <%
  2.     Date d = new Date();
  3.     String dateString = getFormattedDate (d);
  4. %>
Because I dont know any better :-( I change it to a string so I can manipulate it, see below:-

Expand|Select|Wrap|Line Numbers
  1. <%! 
  2.     String getFormattedDate(Date d)
  3.    {
  4.     SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yy hh:mm:ss");
  5.     return simpleDate.format(d);
  6.    }
  7. %>
As long as I can get a date format of dd/MM/yy hh:mm:ss as a DATE I'll be happy!
Mar 14 '08 #7
BigDaddyLH
1,216 Recognized Expert Top Contributor
You want a date string so that you can turn it back into a Date object???

Expand|Select|Wrap|Line Numbers
  1. Date d = new Date();
Why isn't that good enough?
Mar 14 '08 #8
robtyketto
108 New Member
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

Expand|Select|Wrap|Line Numbers
  1. Date d = new Date();
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 ???

Expand|Select|Wrap|Line Numbers
  1. 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
Mar 14 '08 #9
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.
Mar 14 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
4496
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.
1
10713
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 ---------------------
3
22148
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...
5
14719
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...
2
6971
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
4
2514
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.
2
3334
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...
0
2086
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" %> <%
0
9687
marktang
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...
0
9541
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,...
0
10484
Oralloy
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...
0
10251
jinu1996
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...
0
10027
tracyyun
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...
0
9072
agi2029
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...
0
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.