1.JDBC访问Oracle数据库 1 public class Jdbc_Oracle { 2 3 // 静态代码块,只会执行一次,类似C#静态构造方法 4 static { 5 try { 6 // 加载数据库驱动一次 7 Class ...
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") + "\t"); 99 System.out.print(result.getInt(1) + "\t");100 System.out.print(result.getString("Name") + "\t");101 System.out.print(result.getInt("Up") + "\t\n");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") + "\t");163 System.out.print(result.getInt(1) + "\t");164 System.out.print(result.getString("Name") + "\t");165 System.out.print(result.getInt("Up") + "\t\n");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 }
原标题:Java jdbc访问sqlserver,oracle数据库
关键词:JAVA
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。