JDBC--連接數據庫並寫入數據


1. 數據庫連接

 

2. 使用Statement類進行數據庫操作

 

package com.machuang.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo12 {

    public static void main(String[] args) {
        
        Connection conn = null;
        Statement st = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            
            // 建立連接
            conn = DriverManager.getConnection(
                                                "jdbc:mysql://localhost:3306/testjdbc", "root", "333666");
            st = conn.createStatement();
            
            String sql = "insert into t_usr (usrName, pwd, regTime) value ('cappucciono', 464645, now())";
            st.execute(sql);
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(null != st) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(null != conn) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        

    }

}

 

 

3. 使用PreparedStatement類進行數據庫操作

 

package com.machuang.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Demo11 {

    public static void main(String[] args) {
        
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            
            // 建立連接
            conn = DriverManager.getConnection(
                                                "jdbc:mysql://localhost:3306/testjdbc", "root", "333666");
            
            String sql = "insert into t_usr (usrName, pwd, regTime) value (?, ?, ?)";    // ?為占位符
            ps = conn.prepareStatement(sql);
            
            // method 1
            ps.setString(1, "Cappuccino01");        // index 從1開始
            ps.setString(2, "464165");
            ps.setDate(3, new java.sql.Date(System.currentTimeMillis()));
            
            ps.execute();
            
//            // method 2 (不用考慮參數類型)
//            ps.setObject(1, "Cappuccino02");        // index 從1開始
//            ps.setObject(2, "464165");
//            ps.setObject(3, new java.sql.Date(System.currentTimeMillis()));
//            
//            ps.execute();
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(null != ps) {
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(null != conn) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        

    }

}

 

 

4. 測試ResuleSet 結果集

 

package com.machuang.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Demo13 {

    public static void main(String[] args) {
        
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            
            // 建立連接
            conn = DriverManager.getConnection(
                                                "jdbc:mysql://localhost:3306/testjdbc", "root", "333666");
            
            String sql = "select id, usrName, pwd from t_usr where id > ?";
            ps = conn.prepareStatement(sql);
            
            ps.setInt(1, 2);    // 查詢數據庫中第個元素
            
            rs = ps.executeQuery();    // 執行查詢命令
            
            while(rs.next()) {
                System.out.println(rs.getInt(1) + "----"  + rs.getString(2) + "----" +rs.getString(3));
            }
            
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            
            // 按照后打開的先關閉的原則依次關閉 Result-->PreparedStatement-->Connection
            // attention: 3個 trycatch塊分開寫
            try {
                if(null != rs) {
                    rs.close();
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            try {
                if(null != ps) {
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(null != conn) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM