zoukankan      html  css  js  c++  java
  • JDBC 关于Date格式

    package test;

    import java.sql.Connection;
    import java.util.Date;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class SqlDate {

        /**
         * @param args
         * @throws SQLException
         * @throws ClassNotFoundException
         */
        public static void main(String[] args) throws SQLException, ClassNotFoundException {
            // TODO Auto-generated method stub
            create("name2",new Date(),1000.9f);
            System.out.println(getData(1));
        }

        static void create(String name,Date birthday,float money) throws SQLException {
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            try {
                conn = JdbcUtils.getConnection();
                //3,Statement用于“运送”sql语句和sql语句执行结果
                String sql = "insert into user(name,birthday,money) values (?,?,?)";

                st = conn.prepareStatement(sql);
               
                st.setString(1, name);
                st.setDate(2, new java.sql.Date(birthday.getTime()));//将util的Date转换为sql的Date
                st.setFloat(3, money);
                //4,执行sql
                int count = st.executeUpdate();

                System.out.println(count);


            } finally {
                JdbcUtils.free(rs, st, conn);
            }
        }
       
       
        private static Date getData(int id) throws ClassNotFoundException, SQLException {
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            Date bithday = null;
            try {
                conn = JdbcUtils.getConnection();

                st = conn.createStatement();
               
                rs = st.executeQuery("select birthday from user where id = "+id);
               
                while(rs.next()) {
                    bithday = rs.getDate("birthday");
                }
            } finally {
                JdbcUtils.free(rs, st, conn);
            }
           
            return bithday;
        }
       
    }

  • 相关阅读:
    SQL SERVER 全文索引分词
    Json官方介绍
    SQL Try Catch(转载http://www.cnblogs.com/jimmyray/archive/2011/08/02/2125069.html)
    SQL函数记录
    SQL事务处理代码(SQL Server 2000 & 2005)
    SQL通用分页存储过程
    [导入]SoapExtension 1.0 的问题与解决
    BugTracker.NET 汉化手札
    [导入]我对J2EE和.NET的一点理解
    PostgreSQL 8.0.2 应用报告
  • 原文地址:https://www.cnblogs.com/flying607/p/3459512.html
Copyright © 2011-2022 走看看