增删改查

阅读: 评论:0

增删改查

增删改查

增删改查-java(新手)

 PreparedStatement:

方法:

 

 


Connection:

方法:

 

 

 实例:


 

1、查询:

 1 package cn.chuang.JdbcDome;
 2 
 3 import java.sql.*;
 4 
 5 public class JdbcDome3 {
 6     public static void main(String[] args) throws Exception {
 7         PreparedStatement ppst = null;
 8         Connection conn = null;
 9         fun3(ppst,conn);
10     }
11 
12     public static void fun1(PreparedStatement ppst,Connection conn) throws Exception {
13         //查询表的内容
14         //1 注册驱动 获得Connection
15         Class.forName(&#sql.jdbc.Driver");
16         //2 获得链接
17         conn = Connection("jdbc:mysql:///semployee", "root", "root");
18         //3 sql语句
19         String sql = "select * from lll";
20         //4 获得执行sql语句的对象
21         ppst = conn.prepareStatement(sql);
22         ResultSet rs = uteQuery(sql);
23         //5 让游标向下移动一行
24         rs.next();
25         int i = rs.getInt(1);
26         String name = rs.getString("ename");
27         //6 获取数据
28         System.out.println(i+"  "+name);
29     }
30    

 


 

2、添加

 1     public static void fun2(PreparedStatement ppst,Connection conn) throws Exception {
 2         //在表中添加数据,表结构有多少就要写多少。不能漏写,会报错。
 3         try {
 4             //1 注册驱动 获得Connection
 5             Class.forName(&#sql.jdbc.Driver");
 6             //2 获得链接
 7             conn = Connection("Jdbc:mysql:///semployee", "root", "root");
 8 
 9             //3 sql语句
10             String sql = "insert into lll values (null,'兀立扗'),(null,'吴诗意')";
11             //4 获得执行sql语句的对象
12             ppst = conn.prepareStatement(sql);
13             int i = uteUpdate(sql);
14             //5 处理结果
15             System.out.println(i);
16             //6 另创建if语句,做提示用。
17             if (i>0){
18                 System.out.println("添加成功");
19             }else{
20                 System.out.println("添加失败");
21             }
22         } catch (Exception e) {
23             e.printStackTrace();
24         }finally {
25             if(ppst!=null){
26                 try {
27                     ppst.close();
28                 } catch (SQLException e) {
29                     e.printStackTrace();
30                 }
31             }
32             if (conn!=null){
33                 try {
34                     conn.close();
35                 } catch (SQLException e) {
36                     e.printStackTrace();
37                 }
38             }
39         }
40     }
41    

 


 

3、删除 

 public static void fun3(PreparedStatement ppst,Connection conn) throws Exception {//删除表内数据。//1 注册驱动 获得ConnectionClass.forName(&#sql.jdbc.Driver");conn = Connection("jdbc:mysql:///semployee", "root", "root");//2 sql语句String sql = "delete from lll where uid = 2";//3 获得执行sql语句的对象Statementppst = conn.prepareStatement(sql);int i = uteUpdate(sql);//4 处理结果
        System.out.println(i);if (i>0){System.out.println("删除成功");ppst.close();conn.close();}else{System.out.println("删除失败");}}

 


 4、修改

 1 public static void fun4(PreparedStatement ppst,Connection conn) throws Exception {
      //修改表内数据 2 //1 注册驱动。 3 Class.forName(&#sql.jdbc.Driver"); 4 //2 链接数据库。 5 conn = Connection("jdbc:mysql:///semployee", "root", "root"); 6 7 //3 SQL语句。 8 String sql = "update lll set uname = '吴惆' where uid = 1 "; 9 10 //4 获得执行SQL的语句。 11 ppst = conn.prepareStatement(sql); 12 //5 处理结果。 13 int ou = uteUpdate(sql); 14 System.out.println(ou); 15 } 16 }

 

posted @ 2019-04-01 19:52 浪子。 阅读( ...) 评论( ...) 编辑 收藏

本文发布于:2024-02-01 07:17:16,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170674303634828.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23