import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
public class OracleTest {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = OracleConnectionFactory.getConnection();
DatabaseMetaData dmd = con.getMetaData();
System.out.println("database product name: " + dmd.getDatabaseProductName());
System.out.println("database product version: " + dmd.getDatabaseProductVersion());
System.out.println("supports generate keys? " + dmd.supportsGetGeneratedKeys());
String sql = "INSERT INTO t_mt_stat (id, mt_time, stat, stat_time) " +
"VALUES (s_mt_stat.nextval, ?, ?, ?)";
ps = con.prepareStatement(sql, new String[]{"ID"}); // 后面一个参数表示需要返回的列
ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
ps.setString(2, "Y");
ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
ps.executeUpdate();
if(dmd.supportsGetGeneratedKeys()) {
rs = ps.getGeneratedKeys();
while(rs.next()) {
// 如果使用 rs.getInt("ID") 会报错,奇怪的 Oracle JDBC 驱动!
System.out.println("ID: " + rs.getInt(1));
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(rs, ps, con);
}
}
}
分享到:
相关推荐
- **主键**:`PRIMARY KEY`,确保每行数据的唯一性。 - **外键**:`FOREIGN KEY`,确保表之间的参照完整性。 - **唯一**:`UNIQUE`,确保某列值的唯一性。 - **非空**:`NOT NULL`,确保某列值不能为空。 - **检查**...
user_id NUMBER(5) PRIMARY KEY, user_balance NUMBER(9,2), user_name VARCHAR2(20), user_birthday DATE, user_photo BLOB, user_doc CLOB, user_file BFILE, user_age INT ); ``` 2. **插入数据**:...
常见的约束有`primary key`, `foreign key`, `unique`, `check`, `not null`等。 ##### 显示约束信息 - 使用`select * from user_constraints;`查看用户拥有的约束信息。 #### 十一、Oracle索引 - 索引用于提高...
id NUMBER PRIMARY KEY, clob_data CLOB ); ``` 3. 插入CLOB数据: - 使用`EMPTY_CLOB()`函数可以插入一个空的CLOB值,然后通过PL/SQL过程逐块写入数据。 - 直接插入字符串,但需要注意长度不能超过4000字符...
String sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)"; stmt.executeUpdate(sql); ``` 3. **查询数据** - 使用`Statement`的`executeQuery()`方法执行SELECT语句,返回一个`...
String createTableSql = "CREATE TABLE Images (id INT PRIMARY KEY, imageName VARCHAR(100), imageData BLOB)"; PreparedStatement pstmt = conn.prepareStatement(createTableSql); pstmt.executeUpdate(); ...
id INT PRIMARY KEY, imageData BLOB, filename VARCHAR(255) ); ``` 在Servlet中,使用JDBC插入图片数据: ```java Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); ...
PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; Insert INTO `user` VALUES ('1', 'summer', '100', 'shanghai,pudong'); 到此为止,前期准备工作就完成了。下面开始真正配置...
{12.21.1}primary key, unique}{181}{subsection.12.21.1} {12.21.2}unique}{182}{subsection.12.21.2} {12.21.3}foreign key}{182}{subsection.12.21.3} {12.22}view}{187}{section.12.22} {12.23}index, ...