JAVA连接MYSQL,使用PreparedStatement 写查询,修改,添加,删除,语句

http://blog.csdn.net/blacksource/article/details/4380358

添加:

PreparedStatement ps=conn.prepareStatement("insert into Goods (Gname,Gprice,Gamount,Gdate,Gperson) values (?,?,?,?,?)");
            ps.setString(1,goods.getGname());
            ps.setInt(2,goods.getGprice());
            ps.setInt(3,goods.getGamount());
            ps.setString(4,goods.getGdate());
            ps.setString(5,goods.getGperson());
            i=ps.executeUpdate();

查询:

PreparedStatement ps=conn.prepareStatement("select * from USERMSG where username=? and password=?");
            ps.setString(1,username);
            ps.setString(2,password);
            rs=ps.executeQuery();

修改

PreparedStatement ps=conn.prepareStatement("update Goods set Gname=?,Gprice=?,Gamount=?,Gdate=?,Gperson=? where Gid=?");
            ps.setString(1,goods.getGname());
            ps.setInt(2,goods.getGprice());
            ps.setInt(3,goods.getGamount());
            ps.setString(4,goods.getGdate());
            ps.setString(5,goods.getGperson());
            ps.setInt(6,goods.getId());
            i=ps.executeUpdate();

删除:

PreparedStatement ps=conn.prepareStatement("delete from Goods where Gid=?");
            ps.setString(1,username);
            rs=ps.executeQuery();

具体可以参考链接,相似。

你可能感兴趣的:(java,mysql)