我编了个小程序,将表单填入的数据写入数据库,具体实现是从一个HTML转到一个JSP.
用request.getParameter(String)方法得到String对象,但在写入数据库时发生了类型转换的问题.程序执行没有问题,但调用数据库select语句后,数据库对应的位置上都是空白的,Date类型的下面显示的是0000-00-00.
现急求高手解答,如何将String对象转换为sql中的char和date类型?
另外,sql中的char类型和varchar类型有何区别?
如何将连接数据库和操作数据库的行为写成javabean?
用request.getParameter(String)方法得到String对象,但在写入数据库时发生了类型转换的问题.程序执行没有问题,但调用数据库select语句后,数据库对应的位置上都是空白的,Date类型的下面显示的是0000-00-00.
现急求高手解答,如何将String对象转换为sql中的char和date类型?
另外,sql中的char类型和varchar类型有何区别?
如何将连接数据库和操作数据库的行为写成javabean?
解决方案 »
- MyEclipse项目发布后,运行时找不到jsp文件!!!新手急求!!!
- jsp 中通过 按钮传参 但不希望 页面跳转 如何实现啊
- 国外服务器?
- 如何在线编辑word文档
- DriverManager.getConnection(String url, String user, String password)
- 急,高手救命!getOutputStream() Exception
- JAVA WEB关于HTTP请求消息使用get或post方法的问题
- 请教:为什么在Jsp页面中进行有效性检查,不合法时还会跳到另外一个页面去?
- 这是什么错误呀?
- 关于乱码的问题!!!!!!!!
- 怎样改变javabean的存放目录
- 暂且叫他LIST分配问题 :(
String birthdayS=request.getParameter("Birthday");int year = Integer.parseInt(birthdayS.substring(1,4));
int month = Integer.parseInt(birthdayS.substring(6,7));
int day = Integer.parseInt(birthdayS.substring(9,10));
Date birthday = new Date(year, month, day);
java.sql.Date date1=new java.sql.Date(date.getTime());
java.text.SimpleDateFormat dateFormatter=new java.text.SimpleDateFormat();
....
prepStmt.setDate(3,date1);
.....
2 char和varchar都是字符串类型的>>>> char: 固定长度的非 Unicode 字符数据,最大长度为 8,000 个字符。 varchar: 可变长度的非 Unicode 数据,最长为 8,000 个字符。3 p>Description:
* JDBC通用操作类
*
* 关于数据库的主要操作有:获取数据库连接;数据库查询、插入、修改、删除;
* 断开数据库连接。这些数据库操作对于操作不同的数据表应该说都是统一的,
* 因此,数据库的JDBC操作是可以做成一个通用类,这样就能达到重用目的。
*
* </p>
* <p>Copyright: Jdon.com Copyright (c) 2003</p>
* <p>Company: 上海解道计算机技术有限公司</p>
* @author banq
* @version 1.0
*/public class Mysql { private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null; /**
* 以创建Statement 初始化Mysql
*/
public Mysql() {
try {
getDataSource();
stmt = conn.createStatement();
} catch (Exception e) {
System.err.println("Mysql init error: " + e);
}
} private void getDataSource() {
try {
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Boom - No Context"); DataSource ds =
(DataSource) ctx.lookup("java:comp/env/jdbc/userDB");
if (ds != null)
conn = ds.getConnection(); } catch (Exception e) {
System.err.println("getDataSource() error: " + e);
}
} private void getDirectConn(){
try {
Class.forName(Constants.dbdriver).newInstance();
conn = DriverManager.getConnection(Constants.dburl);
} catch (Exception e) {
System.err.println("getDataSource() error: " + e);
} } /**
* 以创建PreparedStatement 初始化Mysql
*/
public Mysql(String sql) {
try {
getDataSource();
prepareStatement(sql);
} catch (Exception e) {
System.err.println("Mysql init error: " + e);
}
} public Connection getConnection() {
return conn;
} public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
} public void setString(int index, String value) throws SQLException {
prepstmt.setString(index, value);
} public void setInt(int index, int value) throws SQLException {
prepstmt.setInt(index, value);
} public void setBoolean(int index, boolean value) throws SQLException {
prepstmt.setBoolean(index, value);
} public void setDate(int index, Date value) throws SQLException {
prepstmt.setDate(index, value);
} public void setLong(int index, long value) throws SQLException {
prepstmt.setLong(index, value);
} public void setFloat(int index, float value) throws SQLException {
prepstmt.setFloat(index, value);
} public void setBinaryStream(int index, InputStream in, int length) throws
SQLException {
prepstmt.setBinaryStream(index, in, length);
} public void clearParameters() throws SQLException {
prepstmt.clearParameters();
} public PreparedStatement getPreparedStatement() {
return prepstmt;
} public Statement getStatement() {
return stmt;
} /**
* 执行Statement查询语句
* @param sql
* @return
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
} else
return null;
} /**
* 执行PreparedStatement查询语句
* @return
* @throws SQLException
*/
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
} else
return null;
} /**
* 执行Statement更改语句
* @param sql
* @throws SQLException
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
} /**
* 执行PreparedStatement更改语句
* @throws SQLException
*/
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
} /**
* 关闭连接
*/
public void close() {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
} catch (Exception e) {
System.err.println("Mysql close error: " + e);
} }
}
package org.springside.framework.utils;import org.springframework.context.i18n.LocaleContextHolder;
import org.springside.framework.Constants;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;/**
* @author calvin
*/
public class DateUtil {
private static String defaultDatePattern = "yyyy-MM-dd"; static { Locale locale = LocaleContextHolder.getLocale();
//尝试试从messages_zh_Cn.properties中获取defaultDatePattner.
try {
defaultDatePattern = ResourceBundle.getBundle(Constants.MESSAGE_BUNDLE_KEY, locale).getString("date.default_format");
}
catch (MissingResourceException mse) {
}
} /**
* get the default date pattern
*/
public static String getDatePattern() {
return defaultDatePattern;
} /**
* returns the current date in the default format
*/
public static String getToday() {
Date today = new Date();
return format(today);
} /**
* convert Date to String in default format
*/
public static String format(Date date) {
return format(date, getDatePattern());
} /**
* convert Date to String in pattern
*/
public static String format(Date date, String pattern) {
String returnValue = ""; if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
} return (returnValue);
} /**
* converts a String to a Date using the default Pattern
*/
public static Date parse(String strDate) throws ParseException {
return parse(strDate, getDatePattern());
} /**
* converts a String to a Date using the pattern
*/
public static Date parse(String strDate, String pattern) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.parse(strDate);
} /**
* add some month from the date
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
}