2020-12-05王永苗

1.加载驱动,2.创建连接,3拼写sql,4.发送sql,5.执行sql,6.关闭资源
package com.itlaobing.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBUtils {

public int save(String username,String password,String gender) throws Exception {
    //1、加载驱动
    Class.forName("com.mysql.jdbc.Driver");
    //2、创建连接
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_user","root","");
    //3、拼写sql
    String sql = "INSERT INTO t_user (userName,`passWord`,gender)VALUES(?,?,?)";
    //4、发送sql
    PreparedStatement prepareStatement = connection.prepareStatement(sql);
    //给占位符赋值
    prepareStatement.setObject(1, username);
    prepareStatement.setObject(2, password);
    prepareStatement.setObject(3, gender);
    //5、执行sql
    int i = prepareStatement.executeUpdate();
    //ResultSet rs = prepareStatement.executeQuery();

// while (rs.next()) {
// System.out.println(rs.getObject("userName"));
// System.out.println(rs.getObject("gender"));
// }
//7、关闭资源
prepareStatement.close();
connection.close();
//方法的返回值
return i;
}
}

标签

评论

© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1