JDBC learning notes (5)

xiaoxiao2021-03-06  77

JDBC learning notes (5)

Thinkersky [email: yanghuangming@rongji.com]

◆ Dad has played me twice today. The first time I saw the transcripts in my hand, the second time was because the transcript was a child.

◆ Tragedy is like I accidentally cut my little fingers; comedy is like you accidentally fall into the sewer.

◆ Here, I will announce

Smith

Mr.'s will, before publishing a will, I want to ask sincerely.

Smith

Lady, is you willing to accept my proposal?

◆ I don't understand why you are not stolen, but it's just about stealing things that don't make money.

Thief: Enough! For this, the wife has fallen me for more than a month.

Well, this is a few funny text messages that take time to see it in the company forum, and share it with everyone.

It is relatively busy today, and learning is relatively small, just a slight review of the transformation of data types between Java and SQL. If you don't know that Java and SQL have a set of data types that have their own defined data (JSP data types are actually Java data type), we must exchange data between JSP programs and database management systems, inevitably The data type of the person is converted. Let us look at two tables first:

Table SQL to Java data type shot table

SQL data types JAVA data type CHAR String VARCHAR String LONGVARCHAR String NUMERIC java.math.BigDecimal DECIMAL java.math.BigDecimal BIT Boolean TINYINT Byte SMALLINT Short INTEGER Int BIGINT Long REAL Float FLOAT Double DOUBLE Double BINARY byte [] VARBINARY byte [] LONGVARBINARY byte [] Date java.sql.date time java.sql.time timestamp java.sql.timestamp

Java to SQL Data Type Show

JAVA data type SQL data type String VARCHAR or LONGVARCHAR java.math.BigDecimal NUMERIC Boolean BIT Byte TINYINT Short SMALLINT Int INTEGER Long BIGINT Float REAL Double DOUBLE byte [] VARBINARY or LONGVARBINARY java.sql.Date DATE java.sql.Time TIME java. SQL.TimeStamp TimeStamp

Here, everyone should pay attention, not all data types are supported in various database management systems. Hereinafter, the transformation between several common data types is described:

(1) Char, VARCHAR, and Longvarchar

In the SQL language, there are three character types of character types of different lengths, VARCHAR, and LONGVARCHAR, and there is no corresponding three different data types in Java / JSP, and the method of processing of JDBC is to String or char [] corresponds to it. Do not distinguish between three SQL data types in actual programming, all of them convert them to Sting or Char []. And usually use the very common String type of the application. We can also use the String class to convert a String object to Char [], or constructed a Stirng object with char [] as parameters.

For SQL Data Type Char (n) of the length, when extracting the data from the result set obtained from the Database Management system, the JDBC will represent a String object that is a length N, if the actual character is The number is not enough, and the system will automatically replenish the String object. When the data type written to the database management system should be char (n), the JDBC also replenishes the corresponding number of spaces at the end of the String object. Under normal circumstances, there is no error between char, varchar, longvarchar, and string. But it is very worth noting that longvarchar, this SQL data type sometimes has several megabytes of data, which exceeds the String object. The JDBC solution is to accept this type of data with Java's INPUT Stream [I will refer to it later]. Input Stream not only supports ASCII, but also supports Unicode, we can choose as needed.

(2) Decimal and NUMERIC

SQL Decimal and NuMERIC are often used to represent a fixed point for a certain precision. In the simple data type of Java, there is no type corresponding to it. However, from JDK1.1, Sun has added a new class BigDecimal in java.math. * Pack, which can be converted with Decimal, NuMeric.

In addition, when data is read from the database management system, you can also use the getString () method to get Decimal and Numeric.

(3) Binary, Varbinary, and Longvarbinary

There is no need to distinguish these three SQL data types when programming, and JDBC will unify them into Byte []. Where longvarbinary and longvarchar are similar, you can represent a few megabytes of data, beyond the band of array. The solution is still useful to accept data with input stream.

(4) Bit

The bit type representing a binary bit is imaged by JDBC to Boolean.

(5) Tinyint, Smallint, INTEGER, and BIGINT

The SQL language Tinyint, Smallint, Integer, and Bigint represent 8-bit, 16-bit, 32-bit, 64-bit data. They are filled with Byte, Short, Int, and Long, Shave, SHORT, INT, and LONG.

(6) REAL, FLOAT, and DOUBLE

SQL defines REAL, FLOAT, and DOUBLE to support floating point. JDBC fuses the REAL to the Java's float, and the float, Double is vivid to Java's Double.

(7) Date, Time, and TimeStamp

SQL defines three and date-related data types. Date represents the year, month, day, time representative, minutes, seconds, timestamp combines all information for Date and Time, and adds more accurate time metering units.

In the standard class library of Java, the DATE class in the java.util. * Is used to represent the date and time. However, Date, TIME, and TIMESTAMP direct material directly is not clear. Also, the class does not support the accurate time metering unit of TimeStamp. Therefore, Sun has added three subclasses for java.util.date in java.utql. *: Java.sql.date, java.sql.time, java.sql.timestamp, three date data in SQL, respectively The type corresponds to the type.

In short, there are many details on SQL and Java, and there are many details, and here will be introduced, and friends who need them can check the relevant documents. Here to introduce you to the website I often go: http://java.sun.com/docs/books/tutorial/jdbc.

转载请注明原文地址:https://www.9cbs.com/read-121482.html

New Post(0)