当前位置:首页 > 数据库 > Oracle

Java jdbc访问sqlserver,oracle数据库

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函数,数据的操作 14publicstaticvoid main(String[] args) {
 15        del();
 16//exec(); 17        select();
 18    }
 19 20// 添加、增删改 21publicstaticvoid exec() {
 22         Connection con = null;
 23         PreparedStatement cmd = null;
 24try {
 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返回影响行数 38int result = cmd.executeUpdate();
 39             System.out.println("影响行数:" + result);
 40         } catch (Exception e) {
 41// 把错误的堆栈信息显示在控制台 42            e.printStackTrace();
 43         } finally {
 44try {
 45                cmd.close();
 46                con.close();
 47             } catch (Exception e) {
 48                e.printStackTrace();
 49            }
 50        }
 51    }
 52 53// 删除、增删改 54publicstaticvoid del() {
 55             Connection con = null;
 56             PreparedStatement cmd = null;
 57try {
 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返回影响行数 65int result = cmd.executeUpdate();
 66                 System.out.println("影响行数:" + result);
 67             } catch (Exception e) {
 68// 把错误的堆栈信息显示在控制台 69                e.printStackTrace();
 70             } finally {
 71try {
 72                    cmd.close();
 73                    con.close();
 74                 } catch (Exception e) {
 75                    e.printStackTrace();
 76                }
 77            }
 78        }
 79 80// 查询 81publicstaticvoid select() {
 82         Connection con = null;
 83         PreparedStatement cmd = null;
 84         ResultSet result = null;
 85try {
 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// 取出结果集中的数据 97while (result.next()) {
 98                 System.out.print(result.getInt("Id") + "t");
 99                 System.out.print(result.getInt(1) + "t");
100                 System.out.print(result.getString("Name") + "t");
101                 System.out.print(result.getInt("Up") + "tn");
102            }
103         } catch (Exception e) {
104            e.printStackTrace();
105         } finally {
106try {
107                result.close();
108                cmd.close();
109                con.close();
110             } catch (Exception e) {
111                e.printStackTrace();
112            }
113        }
114    }
115}
116117// 删除、增删改118publicstaticvoid del() {
119             Connection con = null;
120             PreparedStatement cmd = null;
121try {
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返回影响行数129int result = cmd.executeUpdate();
130                 System.out.println("影响行数:" + result);
131             } catch (Exception e) {
132// 把错误的堆栈信息显示在控制台133                e.printStackTrace();
134             } finally {
135try {
136                    cmd.close();
137                    con.close();
138                 } catch (Exception e) {
139                    e.printStackTrace();
140                }
141            }
142        }
143144// 查询145publicstaticvoid select() {
146         Connection con = null;
147         PreparedStatement cmd = null;
148         ResultSet result = null;
149try {
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// 取出结果集中的数据161while (result.next()) {
162                 System.out.print(result.getInt("Id") + "t");
163                 System.out.print(result.getInt(1) + "t");
164                 System.out.print(result.getString("Name") + "t");
165                 System.out.print(result.getInt("Up") + "tn");
166            }
167         } catch (Exception e) {
168            e.printStackTrace();
169         } finally {
170try {
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) {
  9e.printStackTrace();
 10}
 11}
 12 13publicstaticvoid main(String[] args) {
 14del();
 15//exec(); 16select();
 17}
 18 19// 添加、增删改 20publicstaticvoid exec() {
 21 Connection con = null;
 22 PreparedStatement cmd = null;
 23try {
 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返回影响行数 37int result = cmd.executeUpdate();
 38 System.out.println("影响行数:" + result);
 39 } catch (Exception e) {
 40// 把错误的堆栈信息显示在控制台 41e.printStackTrace();
 42 } finally {
 43try {
 44cmd.close();
 45con.close();
 46 } catch (Exception e) {
 47e.printStackTrace();
 48}
 49}
 50}
 51 52// 删除、增删改 53publicstaticvoid del() {
 54 Connection con = null;
 55 PreparedStatement cmd = null;
 56try {
 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返回影响行数 64int result = cmd.executeUpdate();
 65 System.out.println("影响行数:" + result);
 66 } catch (Exception e) {
 67// 把错误的堆栈信息显示在控制台 68e.printStackTrace();
 69 } finally {
 70try {
 71cmd.close();
 72con.close();
 73 } catch (Exception e) {
 74e.printStackTrace();
 75}
 76}
 77}
 78 79// 查询 80publicstaticvoid select() {
 81 Connection con = null;
 82 PreparedStatement cmd = null;
 83 ResultSet result = null;
 84try {
 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// 取出结果集中的数据 96while (result.next()) {
 97 System.out.print(result.getInt("Id") + "t");
 98 System.out.print(result.getInt(1) + "t");
 99 System.out.print(result.getString("Name") + "t");
100 System.out.print(result.getInt("Up") + "tn");
101}
102 } catch (Exception e) {
103e.printStackTrace();
104 } finally {
105try {
106result.close();
107cmd.close();
108con.close();
109 } catch (Exception e) {
110e.printStackTrace();
111}
112}
113}
114115 }
View Code

 

原文:http://www.cnblogs.com/wwym/p/5574698.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐