利用Java编码实现对oracle数据库的操作

原创
2016/06/19 09:13
阅读数 83

 private static String driver = "oracle.jdbc.driver.OracleDriver";    // 驱动URL jdbc:oracle:thin:@127.0.0.1:1521:monitor    private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:studb";    // 登陆数据库的用户名    private static String uname = "system";    // 登陆数据的密码    private static String upassword = "student";

   public static void testOracleConn() {       try {          // 获得DriverManager          Class.forName(driver);          // 创建连接          Connection conn = DriverManager                .getConnection(url, uname, upassword);          // 测试驱动是否获得          System.out.println(conn);

         // sql语句          // String sql = "select id from student where name = ?";          // Statement          // PreparedStatement pstmt = conn.prepareStatement(sql);          // 注入参数          // pstmt.setString(1, "zzw");

         // sql语句          String sql = "select * from student";          // Statement          Statement stmt = conn.createStatement();          //          ResultSet rs = stmt.executeQuery(sql);          // 循环输出rs          while (rs.next()) {System.out.println("姓名:" + rs.getString(2) + "   年龄:"                  + rs.getInt(3));          }       } catch (ClassNotFoundException | SQLException e) {          // TODO Auto-generated catch block          e.printStackTrace();       }    }

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部