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

2018-06-18 01:59:43来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

    }

}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java类加载的过程

下一篇:Thread源码剖析