Step By Step(Java JDBC篇) (七)

2014-11-24 03:19:38 · 作者: · 浏览: 14
pstmt.setTime(3, sqlTime);

43 pstmt.setTimestamp(4, sqlTimestamp);

44 pstmt.executeUpdate();

45 System.out.println("End inserting.");

46 pstmt.close();

47 // 3. 查询日期型数据

48 String getRecord = "select date_column, time_column, "

49 + "timestamp_column from TestDates where id = ";

50 pstmt = conn.prepareStatement(getRecord);

51 pstmt.setString(1, "001");

52 System.out.println("Begin selecting.");

53 rs = pstmt.executeQuery();

54 while (rs.next()) {

55 java.sql.Date dbSqlDate = rs.getDate(1);

56 java.sql.Time dbSqlTime = rs.getTime(2);

57 java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);

58 System.out.println("dbSqlDate = " + dbSqlDate);

59 System.out.println("dbSqlTime = " + dbSqlTime);

60 System.out.println("dbSqlTimestamp = " + dbSqlTimestamp);

61 }

62 System.out.println("End selecting.");

63 //4. 删除表

64 String dropTable = "drop table TestDates";

65 stmt = conn.createStatement();

66 stmt.execute(dropTable);

67 } catch (Exception e) {

68 e.printStackTrace();

69 } finally {

70 try {

71 rs.close();

72 pstmt.close();

73 conn.close();

74 } catch (SQLException e) {

75 }

76 }

77 }

78 }

7. 获取Table或者View这些数据库内部对象的Metadata信息。通过该种方式获取数据库对象的metadata信息,比直接通过SQL语句查询数据库服务器内置的数据字典表或试图要更加容易在多种数据库之间迁移。还有一种方法用于判断表或试图是否存在,同时还能获取他们所包含columns的信息。执行"select * from " + tableName + " where 1=0" 该查询语句将不会有任何数据返回,但是对于判定该表是否存在,以及各个columns的metadata信息已经是足够了。

1 //获取当前schema包含的table和view的metadata数据。

2 public class MyTest {

3 private static Connection getConnection() throws SQLException,

4 ClassNotFoundException {

5 Class.forName("oracle.jdbc.driver.OracleDriver");

6 Properties conProps = new Properties();

7 conProps.put("user", "sys");

8 conProps.put("password", "helloworld");

9 conProps.put("internal_logon", "sysdba");

10 return DriverManager.getConnection(

11 "jdbc:oracle:thin:@//192.168.1.101:1526/OraHome", conProps);

12 }

13 public static void main(String[] args) throws ClassNotFoundException {

14 Connection conn = null;

15 ResultSet rs = null;

16 try {

17 conn = getConnection();

18 DatabaseMetaData dbmd = conn.getMetaData();

19 String[] types = { "VIEW" ,"TABLE"};

20 //getTables(String catalog, String schemaPattern,

21 // String tableNamePattern, String types[])

22 //其中前三个参数如果为null,表示不进行任何过滤。

23 rs = dbmd.getTables(null, null, "%", types);

24

25 while (