Oracle uses JDBC to add, delete, modify, and check whether the table exists. oraclejdbc

Source: Internet
Author: User

Oracle uses JDBC to add, delete, modify, and check whether the table exists. oraclejdbc
Oracle uses JDBC for addition, deletion, modification, and query

Databases and tables

Table USERS
(
USERNAME VARCHAR2 (20) not null,
PASSWORD VARCHAR2 (20)
)
Alter table USERS
Add constraint U_PK primary key (USERNAME)
// Check whether the table exists
Public boolean validateTableExist (String tableName ){
Int result = 0;
String SQL = "SELECT COUNT (*) FROM USER_OBJECTS WHERE OBJECT_NAME = UPPER ('" + tableName + "')";
Connection conn = this. getSession (). connection ();
Statement st = null;
ResultSet rs = null;
Try {
St = conn. createStatement ();
Rs = st.exe cuteQuery (SQL );
Rs. next ();
Result = rs. getInt (1 );
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
This. util. closeAll (rs, st, conn );
}
Return result = 0? False: true;
}

The complete method of JDBC. The CloseAll method will not be written to you. IF the table exists, True will be returned. IF the table does not exist, False will be returned. It can be directly used in your IF judgment:
If (validateTableExist (tname )){
String sql1 = "insert into" + tname + "values (?,?,?,?,?,?) ";
....................
}


/**
* JdbcExample. java
*
* Provider: CoderDream's Studio
*
* History
* Date (DD/MM/YYYY) Author Description
*----------------------------------------------------------------------------
* Apr 14,200 8 CoderDream Created
*/

Package com. coderdream. jdbc. oracle;

Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;

/**
* @ Author XL
*
*/
Public class JdbcExample {


Private static Connection getConn (){
String driver = "oracle. jdbc. driver. OracleDriver ";
String url = "jdbc: oracle: thin: @ 10.5.15.117: 1521: csi ";
String username = "scott ";
String password = "tiger ";
Connection conn = null;
Try {
Class. forName (driver );
// New oracle. jdbc. driver. OracleDriver ();
Conn = DriverManager. getConnection (url, username, password );
}
Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
Catch (SQLException e ){
E. printStackTrace ();
}

Return conn;
}

Private static int insert (String username, String password ){
Connection conn = getConn ();
Int I = 0;
String SQL = "insert into users (username, password) values (?,?) ";
PreparedStatement pstmt;
Try {
Pstmt = conn. prepareStatement (SQL );
// Statement stat = conn. createStatement ();
Pstmt. setString (1, username );
Pstmt. setString (2, password );
I = pstmt.exe cuteUpdate ();
System. out. println ("resutl:" + I );

Pstmt. close ();
Conn. close ();
}
Catch (SQLException e ){
E. printStackTrace ();
}

Return I;
}

Private static void query (){
Connection conn = getConn ();
String SQL = "select * from users ";
PreparedStatement pstmt;
Try {
Pstmt = conn. prepareStatement (SQL );
ResultSet rs = pstmt.exe cuteQuery ();
While (rs. next ()){
System. out. println ("name:" + rs. getString ("username ")
+ "\ Tpassword:" + rs. getString ("password "));
}

Rs. close ();
Pstmt. close ();
Conn. close ();
}
Catch (SQLException e ){
E. printStackTrace ();
}

}

Private static int update (String oldName, String newPass ){
Connection conn = getConn ();
Int I = 0;
String SQL = "update users set password = '" + newPass
+ "'Where username = '" + oldName + "'"; PreparedStatement pstmt; try {pstmt = conn. prepareStatement (SQL); I = pstmt.exe cuteUpdate (); System. out. println ("resutl:" + I); pstmt. close (); conn. close ();} catch (SQLException e) {e. printStackTrace ();} return I;} private static int delete (String username) {Connection conn = getConn (); int I = 0; string SQL = "delete users where username = '" + username + "'"; PreparedStatement pstmt; try {pstmt = conn. prepareStatement (SQL); I = pstmt.exe cuteUpdate (); System. out. println ("resutl:" + I); pstmt. close (); conn. close ();} catch (SQLException e) {e. printStackTrace ();} return I;}/*** @ param args */public static void main (String [] args) {insert ("CDE", "123 "); insert ("CoderDream", "456"); query (); update ("CoderDream", "456"); query (); delete ("CoderDream "); query ();}}

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.