天天看點

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 + "	" + name + "	" + age + "	" + 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, 值)方法設定資料。