20201103李祖海
(二)/用main测试/
public class {
添加 :属性 方法
/用main测试/
public static void main(String[] args) {
try {
save();
} catch (Exception e) {
e.printStackTrace();
}
}
/*定义添加用户的方法*/
public static void save() 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('宫本武藏','rb123','女')";
//4、发送sql
PreparedStatement prepareStatement = connection.prepareStatement(sql);
//5、执行sql
int i = prepareStatement.executeUpdate();
//6、处理结果
if(i > 0) {
System.out.println("添加成功");
}
//7、关闭资源
prepareStatement.close();
connection.close();
}
}
心得体会:今天的程序编辑比较难掌握,,,理解上也比较困难
近期评论