是否可以在表中使用 JDBC 将布尔值存储在 VARCHAR2 列中并从中检索数据?
是的,在 Oracle 中,您可以将布尔值存储在表中,该表的列数据类型为 VARCHAR2。
如果您这样做,则 true 和 false 值将分别存储为 1 和 0,并作为相同值(分别)检索。
示例
让我们使用CREATE 语句在 Oracle 数据库中创建一个名为**sampleTable** 的表,如下所示:
CREATE TABLE sampleTable( ID INT, ProductName VARCHAR (20) NOT NULL, CustomerName VARCHAR (20) NOT NULL, IsBillDue VARCHAR (20) NOT NULL, DeliveryDate date, Price INT, Location varchar(20) );
该列IsBillDue 指定是否已支付账单。
下面的 JDBC 程序建立与 Oracle 数据库的连接,并填充 sampleTable 表,在类型为 varchar2 的 IsBillDue 列中插入一个布尔值。
示例
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BooleanTest {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//Getting the connection
String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe";
Connection con = DriverManager.getConnection(oracleUrl, "system", "password");
System.out.println("Connection established......");
//Inserting values to a table
String query = "INSERT INTO sampleTable values (?, ?, ?, ?, ?, ?) ";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Key-Board");
pstmt.setString(2, "Raja");
pstmt.setBoolean(3, true);
pstmt.setDate(4, new Date(1567296000000L));
pstmt.setInt(5, 7000);
pstmt.setString(6, "Hyderabad");
pstmt.execute();
pstmt.setString(1, "Earphones");
pstmt.setString(2, "Roja");
pstmt.setBoolean(3, false);
pstmt.setDate(4, new Date(1556668800000L));
pstmt.setInt(5, 2000);
pstmt.setString(6, "Vishakhapatnam");
pstmt.execute();
pstmt.setString(1, "Mouse");
pstmt.setString(2, "Puja");
pstmt.setBoolean(3, true);
pstmt.setDate(4, new Date(1551398399000L));
pstmt.setInt(5, 3000);
pstmt.setString(6, "Vijayawada");
pstmt.execute();
pstmt.setString(1, "Mobile");
pstmt.setString(2, "Vanaja");
pstmt.setBoolean(3, false);
pstmt.setDate(4, new Date(1551395452000L));
pstmt.setInt(5, 9000);
pstmt.setString(6, "Chennai");
pstmt.execute();
System.out.println("Contents of the table: ");
//Retrieving data
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from sampleTable");
while(rs.next()) {
System.out.print("Name: "+rs.getString("ProductName")+", ");
System.out.print("Customer Name: "+rs.getString("CustomerName")+", ");
System.out.print("Dispatch Date: "+rs.getString("ISBILLDUE")+", ");
System.out.print("Delivery Time: "+rs.getTime("DELIVERYDATE")+", ");
System.out.print("Price: "+rs.getInt("PRICE")+", ");
System.out.print("Location: "+rs.getString("LOCATION"));
System.out.println();
}
}
}输出
Connection established...... Contents of the table: Name: Key-Board, Customer Name: Raja, Dispatch Date: 1, Delivery Time: 00:00:00, Price: 7000, Location: Hyderabad Name: Earphones, Customer Name: Roja, Dispatch Date: 0, Delivery Time: 00:00:00, Price: 2000, Location: Vishakhapatnam Name: Mouse, Customer Name: Puja, Dispatch Date: 1, Delivery Time: 00:00:00, Price: 3000, Location: Vijayawada Name: Mobile, Customer Name: Vanaja, Dispatch Date: 0, Delivery Time: 00:00:00, Price: 9000, Location: Chennai
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP