zoukankan      html  css  js  c++  java
  • 增删改查-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("com.mysql.jdbc.Driver");
    16         //2 获得链接
    17         conn = DriverManager.getConnection("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 = ppst.executeQuery(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("com.mysql.jdbc.Driver");
     6             //2 获得链接
     7             conn = DriverManager.getConnection("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 = ppst.executeUpdate(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 注册驱动 获得Connection
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql:///semployee", "root", "root");
            //2 sql语句
            String sql = "delete from lll where uid = 2";
            //3 获得执行sql语句的对象Statement
            ppst = conn.prepareStatement(sql);
            int i = ppst.executeUpdate(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("com.mysql.jdbc.Driver"); 4 //2 链接数据库。 5 conn = DriverManager.getConnection("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 = ppst.executeUpdate(sql); 14 System.out.println(ou); 15 } 16 }
  • 相关阅读:
    微信二维码 场景二维码 用于推送事件,关注等 注册用户 ,经过测试
    简单的 Helper 封装 -- CookieHelper
    简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
    Java反射机制
    Windows Azure Web Site (13) Azure Web Site备份
    Windows Azure Virtual Machine (1) IaaS用户手册
    Windows Azure Web Site (1) 用户手册
    Windows Azure Web Site (12) Azure Web Site配置文件
    Windows Azure Web Site (11) 使用源代码管理器管理Azure Web Site
    Windows Azure Web Site (10) Web Site测试环境
  • 原文地址:https://www.cnblogs.com/lxr521/p/10638500.html
Copyright © 2011-2022 走看看