如何使用JDBC API从Oracle数据库中删除表?
您可以使用INSERT查询将记录插入表中。
语法
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN); Or, INSERT INTO TABLE_NAME VALUES (value1, value2, value3,...valueN);
要使用JDBCAPI将记录插入数据库的表中,您需要-
注册驱动程序:使用DriverManager 类的 registerDriver()方法注册驱动程序类。将驱动程序类名称作为参数传递给它。
建立连接:使用DriverManager 类的getConnection()方法连接到数据库。将URL(字符串),用户名(字符串),密码(字符串)作为参数传递给它。
创建语句:使用Connection 接口的createStatement()方法创建一个Statement对象。
执行查询:使用executeUpdate()
Statement接口的方法执行查询。
让我们使用CREATE语句在Oracle数据库中创建一个带有名称调度 的表,如下所示-
CREATE TABLE dispatches( PRODUCTNAME VARCHAR2(20), CUSTOMERNAME VARCHAR2(20), DISPATCHDATE DATE, DELIVERYTIME TIMESTAMP(6), PRICE NUMBER(38), LOCATION VARCHAR2(20) );
以下JDBC程序建立与Oracle数据库的连接,并在Dispatches表中插入5条记录-
示例
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Time; public class InsertData_Oracle { public static void main(String args[]) throws SQLException { //注册驱动程序 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //获得连接 String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe"; Connection con = DriverManager.getConnection(oracleUrl, "system", "password"); System.out.println("Connection established......"); //将值插入表格 String query = "INSERT INTO dispatches VALUES (?, ?, ?, ?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, "Key-Board"); pstmt.setString(2, "Raja"); pstmt.setDate(3, new Date(1567315800000L)); pstmt.setTime(4, new Time(1567315800000L)); pstmt.setInt(5, 7000); pstmt.setString(6, "Hyderabad"); pstmt.execute(); pstmt.setString(1, "Earphones"); pstmt.setString(2, "Roja"); pstmt.setDate(3, new Date(1556688600000L)); pstmt.setTime(4, new Time(1556688600000L)); pstmt.setInt(5, 2000); pstmt.setString(6, "Vishakhapatnam"); pstmt.execute(); pstmt.setString(1, "Mouse"); pstmt.setString(2, "Puja"); pstmt.setDate(3, new Date(1551418199000L)); pstmt.setTime(4, new Time(1551418199000L)); pstmt.setInt(5, 3000); pstmt.setString(6, "Vijayawada"); pstmt.execute(); pstmt.setString(1, "Mobile"); pstmt.setString(2, "Vanaja"); pstmt.setDate(3, new Date(1551415252000L)); pstmt.setTime(4, new Time(1551415252000L)); pstmt.setInt(5, 9000); pstmt.setString(6, "Chennai"); pstmt.execute(); pstmt.setString(1, "Headset"); pstmt.setString(2, "Jalaja"); pstmt.setDate(3, new Date(1554529139000L)); pstmt.setTime(4, new Time(1554529139000L)); pstmt.setInt(5, 6000); pstmt.setString(6, "Goa"); pstmt.execute(); System.out.println("Records inserted......"); } }
输出结果
Connection established...... Records inserted......
如果使用select语句验证分发表的内容,则可以在其中找到插入的记录,如下所示:
SQL> select * from dispatches; PRODUCTNAME CUSTOMERNAME DISPATCHDATE DELIVERYTIME PRICE LOCATION ------------------------------------------------------------------------------------------ Key-Board Raja 01-SEP-19 01-SEP-19 11.00.00.000000 AM 7001 Hyderabad Earphones Roja 01-MAY-19 01-MAY-19 11.00.00.000000 AM 2000 Vishakhapatnam Mouse Puja 01-MAR-19 01-MAR-19 10.59.59.000000 AM 3000 Vijayawada Mobile Vanaja 01-MAR-19 01-MAR-19 10.10.52.000000 AM 9001 Chennai Headset Jalaja 06-APR-19 06-APR-19 11.08.59.000000 AM 6000 Goa