您现在的位置是:主页 > news > 网站空间指的是什么意思/如何做网络销售产品

网站空间指的是什么意思/如何做网络销售产品

admin2025/6/4 23:38:33news

简介网站空间指的是什么意思,如何做网络销售产品,线上推广方法有哪些,做数学题的网站JDBC的CRUD操作 JDBC的CRUD操作就是指利用JAVA编程实现数据库(mysql)的增删改查。 首先需要一个连接数据库的类或文件,里面放对应url,user,pwd及驱动。 driver com.mysql.cj.jdbc.Driver url jdbc:mysql://localho…

网站空间指的是什么意思,如何做网络销售产品,线上推广方法有哪些,做数学题的网站JDBC的CRUD操作 JDBC的CRUD操作就是指利用JAVA编程实现数据库(mysql)的增删改查。 首先需要一个连接数据库的类或文件,里面放对应url,user,pwd及驱动。 driver com.mysql.cj.jdbc.Driver url jdbc:mysql://localho…

JDBC的CRUD操作

JDBC的CRUD操作就是指利用JAVA编程实现数据库(mysql)的增删改查。
首先需要一个连接数据库的类或文件,里面放对应url,user,pwd及驱动。

driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3308/db_ishop?serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false
user = root
pass = 123456

再通过connection对象连接数据库

 public int createGoodsType(GoodsType goodsType){//定义sql语句String sql = "insert into t_goodstype (typename) values(?)";//获取数据库连接对象Connection conn = super.getConnection();//创建preparedstatement:1性能更好2.防止SQL注入PreparedStatement ps = null;int result = 0;try {ps = conn.prepareStatement(sql);//给占位符赋值ps.setString(1,goodsType.getTypeName());//因为是inster操作,则执行updateresult = ps.executeUpdate(); //如果成功,返回数据条数} catch (SQLException throwables) {throwables.printStackTrace();}finally {//统一释放连接资源super.closeConnection(null,ps,conn);}return result;};public int updateGoodsType(GoodsType goodsType){//定义sql语句String sql = "update t_goodstype set typeName = ? where typeid = ?";//获取数据库连接对象Connection conn = super.getConnection();//创建preparedstatement:1性能更好2.防止SQL注入PreparedStatement ps = null;int result = 0;try {ps = conn.prepareStatement(sql);//给占位符赋值ps.setString(1,goodsType.getTypeName());ps.setInt(2,goodsType.getTypeID());//因为是inster操作,则执行updateresult = ps.executeUpdate(); //如果成功,返回数据条数} catch (SQLException throwables) {throwables.printStackTrace();}finally {//统一释放连接资源super.closeConnection(null,ps,conn);}return result;};public int removeGoodsType(GoodsType goodsType){//定义sql语句String sql = "delete from t_goodstype where typeid = ?";//获取数据库连接对象Connection conn = super.getConnection();//创建preparedstatement:1性能更好2.防止SQL注入PreparedStatement ps = null;int result = 0;try {ps = conn.prepareStatement(sql);//给占位符赋值ps.setInt(1,goodsType.getTypeID());//因为是inster操作,则执行updateresult = ps.executeUpdate(); //如果成功,返回数据条数} catch (SQLException throwables) {throwables.printStackTrace();}finally {//统一释放连接资源super.closeConnection(null,ps,conn);}return result;};public List<GoodsType> getGoodsType(){//定义sql语句String sql = "select * from t_goodstype;";//获取数据库连接对象Connection conn = super.getConnection();//创建preparedstatement:1性能更好2.防止SQL注入PreparedStatement ps = null;ResultSet rs = null;int result = 0;List<GoodsType> list = new ArrayList<GoodsType>();try {ps = conn.prepareStatement(sql);//给占位符赋值rs = ps.executeQuery();//因为是inster操作,则执行updatewhile (rs.next()){//将结果集中的每一条对象都封装成一个GoodsType对象GoodsType gt = new GoodsType();gt.setTypeID(rs.getInt("typeid"));gt.setTypeName(rs.getString("typeName"));//将封装的对象存入列表list.add(gt);}} catch (SQLException throwables) {throwables.printStackTrace();}finally {//统一释放连接资源super.closeConnection(null,ps,conn);}//返回列表return list;};

以上就是JDBD的CRUD实现过程,我们不难发向除了查询操作,其他都是调用executeUpdate()这个方法,而查询操作则是调用了executeQuery()方法,在这个程序中并没有用到泛用性最强的execute()方法,因为它使用复杂且性能较差。
在定义SQL语句的时候,我们可以利用占位符‘?’来增加我们语句的通用性,让它不是一个只能执行一次的程序,同时注意在给占位符赋值时,序号从1开始,不是0。在写查询方法时我们这里利用了List集合,将对应的数据表存入集合当中,便于管理。

private static void createUI(){Scanner scanner = new Scanner(System.in);System.out.println("=====请添加商品类别=====");System.out.println("请输入商品类别");String typeName = scanner.nextLine();GoodsType gt = new GoodsType();gt.setTypeName(typeName);GoodsTypeDao tgd = new GoodsTypeDao();int result = tgd.createGoodsType(gt);if (result > 0){System.out.println("添加成功");}else {System.out.println("添加失败");}}

以添加为例,在Test函数中,我们只需要核实executeUpdate()的返回值是非为1,便可知道数据处理是否成功。

private static void selectUI(){GoodsTypeDao tgt = new GoodsTypeDao();List<GoodsType> list = tgt.getGoodsType();for (GoodsType gt : list){System.out.print(gt.getTypeID()+"\t");System.out.println(gt.getTypeName());}}

而查询就是在已经添加好的集合当中调取对应的数据即可。

在JDBC发CRUD操作中,我们只要利用好占位符,就可以大幅度的简化编程的复杂度。