Wrox Programmer Forums
|
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 3rd, 2004, 08:01 AM
Authorized User
 
Join Date: Oct 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default prob........

i have a jsp page, from that i can insert date in database..



my form is working.. and my database accept the date in dd-MMM-yy

format...which is working,



but the prob is that it does not display in same pattern means dd-MMM-yy format.



i want to display date in dd-MMM-yy format.i.e 26-jan-04.



it displays date values in yyyy-mm-dd format.



help me.. my code is as follow:




<html>
<body>
<table>
<tr>
<td>

<%@ page import =" java.sql.Date.*" %>
<%@ page import =" java.text.SimpleDateFormat.*" %>
<%@ page import =" java.util.Date.*" %>
<%@ page import="java.text.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page language="java" import="java.sql.*" %>



<%
try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:pf","s cott","ttlscott");
System.out.println("got connection");

%>

<%

String action = request.getParameter("action");
if (action != null && action.equals("save")) {
conn.setAutoCommit(false);



String pattern = "dd/MM/yyyy";

SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setLenient(false);




PreparedStatement pstmt = conn.prepareStatement(
("INSERT INTO pay_header VALUES (?, ?, ?, ?, ?, ?,?,?)"));

pstmt.setString(1,request.getParameter("vou_no"));
java.util.Date dt = sdf.parse(request.getParameter("pay_date"));
pstmt.setDate(2, new java.sql.Date(dt.getTime()));

char pay_post;
if (request.getParameter("pay_post") != null) {pay_post = 'Y';} else {pay_post = 'N';}
pstmt.setString(3,request.getParameter("pay_post") );
pstmt.setString(4,request.getParameter("pay_narr") );
pstmt.setString(5, request.getParameter("chq_no"));
java.util.Date dt1 = sdf.parse(request.getParameter("chq_date"));
pstmt.setDate(6, new java.sql.Date(dt1.getTime()));
pstmt.setFloat(7,Float.parseFloat(request.getParam eter("chq_amt")));
pstmt.setString(8,request.getParameter("pay_type") );



pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
}




%>


<%

Statement statement = conn.createStatement();


ResultSet rs = statement.executeQuery
("SELECT * FROM pay_header ");



%>

<table>
<tr>
<th>Code</th>
<th>Description</th>
<th>Dr. Amt</th>
<th>Cr. Amt</th>
<th>Type</th>
<th>Pct</th>
<th>Pct</th>
</tr>
<tr>
<form action="payment_save1.jsp" method="get">
<input type="hidden" value="save" name="action">
<th><input value="<%= request.getParameter("voucherno") %>" name="vou_no" size="10"></th>
<th><input value="<%= request.getParameter("date") %>" name="pay_date" size="10"></th>
<th><input value="<%= request.getParameter("hold") %>" name="pay_post" size="15"></th>
<th><input value="<%= request.getParameter("narration") %>" name="pay_narr" size="15"></th>
<th><input value="<%= request.getParameter("chqno") %>" name="chq_no" size="15"></th>
<th><input value="<%= request.getParameter("chqdate") %>" name="chq_date" size="15"></th>
<th><input value="<%= request.getParameter("chqamt") %>" name="chq_amt" size="15"></th>
<th><input value="<%= request.getParameter("post") %>" name="pay_type" size="15"></th>
<th><input type="submit" value="save"></th>
</form>
</tr>

<%

while ( rs.next() ) {

%>



<tr>
<form action="payment_save1.jsp" method="get">
<input type="hidden" value="save" name="action">
<td><input value="<%= rs.getString("vou_no") %>" name="vou_no"></td>
<td><input value="<%= rs.getDate("pay_date") %>" name="gl_descr"></td>
<td><input value="<%= rs.getString("pay_post") %>" name="pay_post"></td>
<td><input value="<%= rs.getString("pay_narr") %>" name="pay_narr"></td>
<td><input value="<%= rs.getString("chq_no") %>" name="chq_no"></td>
<td><input value="<%= rs.getDate("chq_date") %>" name="chq_date"></td>
<td><input value="<%= rs.getFloat("chq_amt") %>" name="chq_amt"></td>
<td><input value="<%= rs.getString("pay_type") %>" name="pay_type"></td>


<td><input type="submit" value="save"></td>
</form>
</tr>


<%
}
%>
</table>



<%
// Close the ResultSet
rs.close();

// Close the Statement
statement.close();

// Close the Connection
conn.close();
} catch (SQLException sqle) {
out.println(sqle.getMessage());
} catch (Exception e) {
out.println(e.getMessage());
}
%>
</td>
</tr>
</body>
</html>




 
Old December 3rd, 2004, 08:20 AM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT DATE_FORMAT(myDateField, '%d-%m-%Y') FROM myTable
or

this link may help u

[email protected]
phone -no 91-80-9886566853
 
Old December 3rd, 2004, 08:21 AM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT DATE_FORMAT(myDateField, '%d-%m-%Y') FROM myTable
or

this link may help u
http://www.databasejournal.com/scrip...le.php/1498551

[email protected]
phone -no 91-80-9886566853
 
Old December 4th, 2004, 03:21 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The solution posted by harshagowda shows how to format data as it is retreived, but is there a way to change the format in MySql. I have the same problem, using yyyymmdd. I can change it in jsp, but its a pain having to format in both directions reading and writing to the database. This has to be done with every page that accesses the field.

Can the format in MySql be changed??






Similar Threads
Thread Thread Starter Forum Replies Last Post
validationsummary prob leo_vinay ASP.NET 1.0 and 1.1 Basics 1 October 7th, 2005 12:39 PM
addBatch() prob.......... abhit_kumar JSP Basics 0 December 30th, 2004 03:16 AM
prob in retrieving abhit_kumar MySQL 1 December 24th, 2004 09:03 AM
updation prob.......... abhit_kumar JSP Basics 0 December 14th, 2004 08:07 AM
updation prob.......... abhit_kumar MySQL 0 December 14th, 2004 08:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.