zoukankan      html  css  js  c++  java
  • Java jdbc访问sqlserver,oracle数据库 DEMO

    1.JDBC访问Oracle数据库

      1 public class Jdbc_Oracle {
      2 
      3     // 静态代码块,只会执行一次,类似C#静态构造方法
      4     static {
      5         try {
      6             // 加载数据库驱动一次
      7             Class.forName("oracle.jdbc.driver.OracleDriver");
      8         } catch (ClassNotFoundException e) {
      9             e.printStackTrace();
     10         }
     11     }
     12 
     13     //main函数,数据的操作
     14     public static void main(String[] args) {
     15         del();
     16         //exec();
     17         select();
     18     }
     19 
     20     // 添加、增删改
     21     public static void exec() {
     22         Connection con = null;
     23         PreparedStatement cmd = null;
     24         try {
     25             // 在控制台输入
     26             Scanner scanner = new Scanner(System.in);
     27             System.out.print("请输入类型名称:");
     28             String name = scanner.nextLine();
     29 
     30             // 建立数据库连接,指定数据库用户名,密码,数据库名称
     31             con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
     32             // 创建sql命令对象
     33             cmd = con.prepareStatement("insert into ProductType(Name,Up) values(?,?)");
     34             // 设置参数
     35             cmd.setString(1, name);
     36             cmd.setInt(2, 0);
     37             // 执行sql返回影响行数
     38             int result = cmd.executeUpdate();
     39             System.out.println("影响行数:" + result);
     40         } catch (Exception e) {
     41             // 把错误的堆栈信息显示在控制台
     42             e.printStackTrace();
     43         } finally {
     44             try {
     45                 cmd.close();
     46                 con.close();
     47             } catch (Exception e) {
     48                 e.printStackTrace();
     49             }
     50         }
     51     }
     52     
     53         // 删除、增删改
     54         public static void del() {
     55             Connection con = null;
     56             PreparedStatement cmd = null;
     57             try {
     58                 // 建立数据库连接,指定数据库用户名,密码,数据库名称
     59                 con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
     60                 // 创建sql命令对象
     61                 cmd = con.prepareStatement("delete from ProductType where Id=?");
     62                 // 设置参数
     63                 cmd.setInt(1, 21);
     64                 // 执行sql返回影响行数
     65                 int result = cmd.executeUpdate();
     66                 System.out.println("影响行数:" + result);
     67             } catch (Exception e) {
     68                 // 把错误的堆栈信息显示在控制台
     69                 e.printStackTrace();
     70             } finally {
     71                 try {
     72                     cmd.close();
     73                     con.close();
     74                 } catch (Exception e) {
     75                     e.printStackTrace();
     76                 }
     77             }
     78         }
     79 
     80     // 查询
     81     public static void select() {
     82         Connection con = null;
     83         PreparedStatement cmd = null;
     84         ResultSet result = null;
     85         try {
     86             // 建立数据库连接,指定数据库用户名,密码,数据库名称
     87             con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
     88             // 创建sql命令对象
     89             cmd = con.prepareStatement(
     90                     "select id, name, up from producttype where Id>? and Name like ?");
     91             // 设置参数
     92             cmd.setInt(1, 5);
     93             cmd.setString(2, "%能%");
     94             // 执行sql获得结果集
     95             result = cmd.executeQuery();
     96             // 取出结果集中的数据
     97             while (result.next()) {
     98                 System.out.print(result.getInt("Id") + "	");
     99                 System.out.print(result.getInt(1) + "	");
    100                 System.out.print(result.getString("Name") + "	");
    101                 System.out.print(result.getInt("Up") + "	
    ");
    102             }
    103         } catch (Exception e) {
    104             e.printStackTrace();
    105         } finally {
    106             try {
    107                 result.close();
    108                 cmd.close();
    109                 con.close();
    110             } catch (Exception e) {
    111                 e.printStackTrace();
    112             }
    113         }
    114     }
    115 }
    116 
    117         // 删除、增删改
    118         public static void del() {
    119             Connection con = null;
    120             PreparedStatement cmd = null;
    121             try {
    122                 // 建立数据库连接,指定数据库用户名,密码,数据库名称
    123                 con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "gmall", "orcl");
    124                 // 创建sql命令对象
    125                 cmd = con.prepareStatement("delete from ProductType where Id=?");
    126                 // 设置参数
    127                 cmd.setInt(1, 21);
    128                 // 执行sql返回影响行数
    129                 int result = cmd.executeUpdate();
    130                 System.out.println("影响行数:" + result);
    131             } catch (Exception e) {
    132                 // 把错误的堆栈信息显示在控制台
    133                 e.printStackTrace();
    134             } finally {
    135                 try {
    136                     cmd.close();
    137                     con.close();
    138                 } catch (Exception e) {
    139                     e.printStackTrace();
    140                 }
    141             }
    142         }
    143 
    144     // 查询
    145     public static void select() {
    146         Connection con = null;
    147         PreparedStatement cmd = null;
    148         ResultSet result = null;
    149         try {
    150             // 建立数据库连接,指定数据库用户名,密码,数据库名称
    151             con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "gmall", "orcl");
    152             // 创建sql命令对象
    153             cmd = con.prepareStatement(
    154                     "select id, name, up from producttype where Id>? and Name like ?");
    155             // 设置参数
    156             cmd.setInt(1, 5);
    157             cmd.setString(2, "%能%");
    158             // 执行sql获得结果集
    159             result = cmd.executeQuery();
    160             // 取出结果集中的数据
    161             while (result.next()) {
    162                 System.out.print(result.getInt("Id") + "	");
    163                 System.out.print(result.getInt(1) + "	");
    164                 System.out.print(result.getString("Name") + "	");
    165                 System.out.print(result.getInt("Up") + "	
    ");
    166             }
    167         } catch (Exception e) {
    168             e.printStackTrace();
    169         } finally {
    170             try {
    171                 result.close();
    172                 cmd.close();
    173                 con.close();
    174             } catch (Exception e) {
    175                 e.printStackTrace();
    176             }
    177         }
    178     }
    179 }
    View Code

    2.JDBC访问sqlserver数据库

      1 public class Jdbc_SQLServer {
      2 
      3 // 静态代码块,只会执行一次,类似C#静态构造方法
      4 static {
      5 try {
      6 // 加载驱动一次
      7 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      8 } catch (ClassNotFoundException e) {
      9 e.printStackTrace();
     10 }
     11 }
     12 
     13 public static void main(String[] args) {
     14 del();
     15 //exec();
     16 select();
     17 }
     18 
     19 // 添加、增删改
     20 public static void exec() {
     21 Connection con = null;
     22 PreparedStatement cmd = null;
     23 try {
     24 // 在控制台输入
     25 Scanner scanner = new Scanner(System.in);
     26 System.out.print("请输入类型名称:");
     27 String name = scanner.nextLine();
     28 
     29 // 建立数据库连接,指定数据库用户名,密码,数据库名称
     30 con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
     31 // 创建sql命令对象
     32 cmd = con.prepareStatement("insert into [ProductType]([Name],Up) values(?,?)");
     33 // 设置参数
     34 cmd.setString(1, name);
     35 cmd.setInt(2, 0);
     36 // 执行sql返回影响行数
     37 int result = cmd.executeUpdate();
     38 System.out.println("影响行数:" + result);
     39 } catch (Exception e) {
     40 // 把错误的堆栈信息显示在控制台
     41 e.printStackTrace();
     42 } finally {
     43 try {
     44 cmd.close();
     45 con.close();
     46 } catch (Exception e) {
     47 e.printStackTrace();
     48 }
     49 }
     50 }
     51 
     52 // 删除、增删改
     53 public static void del() {
     54 Connection con = null;
     55 PreparedStatement cmd = null;
     56 try {
     57 // 建立数据库连接,指定数据库用户名,密码,数据库名称
     58 con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
     59 // 创建sql命令对象
     60 cmd = con.prepareStatement("delete from [ProductType] where Id=?");
     61 // 设置参数
     62 cmd.setInt(1, 12);
     63 // 执行sql返回影响行数
     64 int result = cmd.executeUpdate();
     65 System.out.println("影响行数:" + result);
     66 } catch (Exception e) {
     67 // 把错误的堆栈信息显示在控制台
     68 e.printStackTrace();
     69 } finally {
     70 try {
     71 cmd.close();
     72 con.close();
     73 } catch (Exception e) {
     74 e.printStackTrace();
     75 }
     76 }
     77 }
     78 
     79 // 查询
     80 public static void select() {
     81 Connection con = null;
     82 PreparedStatement cmd = null;
     83 ResultSet result = null;
     84 try {
     85 // 建立数据库连接,指定数据库用户名,密码,数据库名称
     86 con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
     87 // 创建sql命令对象
     88 cmd = con.prepareStatement(
     89 "SELECT [Id],[Name],[Up] FROM [GoMall].[dbo].[ProductType] where Id>? and Name like ?");
     90 // 设置参数
     91 cmd.setInt(1, 5);
     92 cmd.setString(2, "%能%");
     93 // 执行sql获得结果集
     94 result = cmd.executeQuery();
     95 // 取出结果集中的数据
     96 while (result.next()) {
     97 System.out.print(result.getInt("Id") + "	");
     98 System.out.print(result.getInt(1) + "	");
     99 System.out.print(result.getString("Name") + "	");
    100 System.out.print(result.getInt("Up") + "	
    ");
    101 }
    102 } catch (Exception e) {
    103 e.printStackTrace();
    104 } finally {
    105 try {
    106 result.close();
    107 cmd.close();
    108 con.close();
    109 } catch (Exception e) {
    110 e.printStackTrace();
    111 }
    112 }
    113 }
    114 
    115 }
    View Code
  • 相关阅读:
    第五周总结
    10.24号进度报告
    10.23日进度报告
    10.22日进度报告
    10.21日进度报告
    10.20号进度总结
    10.19日进度总结
    第四周总结
    10.18日进度博客
    2020下第六周总结
  • 原文地址:https://www.cnblogs.com/wwym/p/5574698.html
Copyright © 2011-2022 走看看