public class JdbcInstrt {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstem = null;
        String url = "jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=utf8";
        String username = "root";
        String password = "12345";

        // 加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
            String sql = "insert into tb_user values(?,?,?,?,?,?,?,?,?,?)";
            pstem = conn.prepareStatement(sql);

            pstem.setInt(1, 3);
            pstem.setString(2, "hdh");
            pstem.setString(3, "12345");
            pstem.setString(4, "root");
            pstem.setInt(5, 22);
            pstem.setString(6, "男");
            pstem.setDate(7, new java.sql.Date(new Date().getTime()));
            pstem.setString(8, "哈哈");
            pstem.setDate(9, new java.sql.Date(new Date().getTime()));
            pstem.setDate(10, new java.sql.Date(new Date().getTime()));

            int result = pstem.executeUpdate();
            System.out.println(result);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // 获取连接
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (pstem != null) {
                try {
                    pstem.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        }
    }

 

posted on 2019-05-01 14:57  忆夏KhaZix  阅读(125)  评论(0编辑  收藏  举报