java通过jdbc连接数据库并更新数据(包括java.util.Date类型数据的更新)

一、步骤

1.获取Date实例,并通过getTime()方法获得毫秒数;

2.将获取的毫秒数存储到数据库中,注意存储类型为nvarchar(20);

3.读取数据库的毫秒数,作为Date构造方法的参数创建实例,有需要再转换时间格式。

 

二、代码示例

package com.yh.dao;

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

import com.yh.util.ConfigManager;

public class NewsDao {
    public static void main(String[] args) {
        NewsDao nd = new NewsDao();
        nd.addData(005, "洪旭", 21, new Date().getTime());
        nd.getData();
    }

    // 查询数据
    public void getData() {
        Connection con = null;
        ConfigManager configManager = ConfigManager.getInstance();
        String url = configManager.getString("jdbc.connection.url");
        String username = configManager.getString("jdbc.connection.username");
        String password = configManager.getString("jdbc.connection.password");
        String driver = configManager.getString("jdbc.SQLServerDriver.class");

        try {
            // 加载驱动
            Class.forName(driver);
            // 获得数据库连接
            con = DriverManager.getConnection(url, username, password);
            // 执行sql语句
            String sql = "select * from student";
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                int age = rs.getInt(3);
                long createDate = rs.getLong(4);
                Date date = new Date(createDate);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(id + "\t" + name + "\t" + age + "\t" + sdf.format(date));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // 插入数据
    public void addData(int id, String name, int age, long createDate) {
        Connection con = null;
        ConfigManager configManager = ConfigManager.getInstance();
        String url = configManager.getString("jdbc.connection.url");
        String username = configManager.getString("jdbc.connection.username");
        String password = configManager.getString("jdbc.connection.password");
        String driver = configManager.getString("jdbc.SQLServerDriver.class");
        try {
            // 加载驱动
            Class.forName(driver);
            // 获得数据库连接
            con = DriverManager.getConnection(url, username, password);
            // 执行sql语句
            String sql = "insert into student (id,name,age,createDate)values(?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            ps.setString(2, name);
            ps.setInt(3, age);
            ps.setLong(4, createDate);
            int line = ps.executeUpdate();
            System.out.println("影响行数:" + line);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
ps.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

 

三、Statement和PreparedStatement的比较

1.Statement由Connection类的createStatement()方法创建,用于发送相对简单,参数较少的sql语句;

2.PreparedStatement由Connection类的preparedStatement()方法创建,用于发送参数较多的sql语句;

3.sql语句使用 " ? " 作为数据占位符,使用set***(int index, 值)方法设置数据。

posted @ 2019-05-19 15:40  想看云飞却没风~  阅读(2255)  评论(0编辑  收藏  举报