欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java--JDBC--连接数据库并写入数据

程序员文章站 2022-10-06 18:39:47
1. 数据库连接 2. 使用Statement类进行数据库操作 3. 使用PreparedStatement类进行数据库操作 4. 测试ResuleSet 结果集 ......

1. 数据库连接

java--JDBC--连接数据库并写入数据

 

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();
            }
        }
        
        

    }

}