JDBC connects to the Oracle program and implements data query. Here is one: Driver A, data source name B, user name C, password D, database table T. Please use JDBC to retrieve all data in table t.

Source: Internet
Author: User
The following describes the general process of using JDBC:
1. Load the driver class of a database, usually using class. forname ("Driver Class Name");
2. Connect to the database connection con = drivermanager. getconnection (URL, username, password );
3. Get the session-statement stmt = con. createstatement ();
4. execute the operation-result rs = stmt.exe cutequery ("SQL query statement ");
5. processing result ――
While (Rs. Next ()){
String col1 = Rs. getstring (1 );
......
}
The general changes in JDBC are:
1. Three types of statement can be created by the connection object: Statement, preparedstatement, and callablestatement.
2. multiple types of resultset can be created: one-way movement and free movement are supported; updatable and unupdatable; transactions of different levels are supported .....
3. batch processing of data input.
4. Operations on special types of columns (blob, clob, arrary, ref, and struct) in the result set.
5. These special types of input databases.
6. Special result set (cachedrowset, jdbcrowset, and webrowset) operations in the javax. SQL package.
7. The others are a datasource, a managed object in J2EE.
Import java. SQL .*;
Public class JDBC {
String dburl = "JDBC: oracle: thin: @ 127.0.0.1: 1521: orcl ";
String theuser = "admin ";
String thepw = "manager ";
Connection c = NULL;
Statement conn;
Resultset rs = NULL;
Public JDBC (){
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
C = drivermanager. getconnection (dburl, theuser, thepw );
Conn = C. createstatement ();
} Catch (exception e ){
E. printstacktrace ();
}}
Public Boolean executeupdate (string SQL ){
Try {
Conn.exe cuteupdate (SQL );
Return true;
} Catch (sqlexception e ){
E. printstacktrace ();
Return false;
}}
Public resultset executequery (string SQL ){
Rs = NULL;
Try {
Rsw.conn.exe cutequery (SQL );
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return Rs;
}
Public void close (){
Try {
Conn. Close ();
C. Close ();
} Catch (exception e ){
E. printstacktrace ();
}}
Public static void main (string [] ARGs ){
Resultset RS;
JDBC conn = new JDBC ();
Rsw.conn.exe cutequery ("select * from test ");
Try {
While (Rs. Next ()){
System. Out. println (Rs. getstring ("ID "));
System. Out. println (Rs. getstring ("name "));
}
} Catch (exception e ){
E. printstacktrace ();
}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.