JDBC Connection Oracle Delete change check

Source: Internet
Author: User
Package com.db.conn;


Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;


public class Conoracl {


Database-driven Classes
Private String Dbdriver = "Oracle.jdbc.driver.OracleDriver";
Connection Database URL
Private String Dburl = "Jdbc:oracle:thin: @localhost: 1521:xe";
Connect database user Name
Private String Dbuser = "System";
Connect Database Password
Private String dbpwd = "123";
Get database connection method, return connection object
private Connection con = null;
Data Execution Statement
Private Statement stat = null;
Private String Sql=null;
Private ResultSet Rs=null;
Creating a database connection
Public Connection Getdbconnect () {
try {
Load Database Driver
Class.forName (Dbdriver);
con = drivermanager.getconnection (Dburl, Dbuser, dbpwd);
catch (Exception e) {
System.out.println (e);
}
return con;
}
Increase
public void Add ()
{
Con=getdbconnect ();
Sql= "INSERT into student (stuno,stuname,stusex)" +
"VALUES (' 1 ', ' Lucy ', ' W ')";
try {
Stat=con.createstatement ();
Stat.executeupdate (SQL);
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Delete
public void Delete ()
{
Con=getdbconnect ();
Sql= "Delete from student" +
"Where stuno=1";
try {
Stat=con.createstatement ();
Stat.executeupdate (SQL);
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
Modify
public void Update ()
{
Con=getdbconnect ();
sql= "Update student set Stuname= ' Ben ' where stuno= ' 2 '";
try {
Stat=con.createstatement ();
catch (SQLException e) {
E.printstacktrace ();
}
}
Inquire
public void Search () {
con = Getdbconnect ();
To create a table's SQL statement
sql = "SELECT * from student";
try {
Stat = Con.createstatement ();
rs= stat.executequery (SQL);
while (Rs.next ()) {
String Stuno = rs.getstring (1);
String stuname = rs.getstring (2);
String stusex = rs.getstring (3);
System.out.println (Stuno + "," + Stuname + "," + stusex);
}
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Function: Close the connection to the database

public void Close () {//6. Releasing resources
try {//catch exception
try {
if (rs!= null) {//When the instance Rs of the ResultSet object is not empty
Rs.close (); Close ResultSet Object
}
finally {
try {
if (stat!= null) {//When an instance of the statement object stmt is not empty
Stat.close (); Close Statement Object
}
finally {
if (con!= null) {//When an instance of the Connection object conn is not empty
Con.close (); Close Connection Object
}
}
}
catch (Exception e) {
E.printstacktrace (System.err); Output exception information
}
}
public static void Main (string[] args) {
Conoracl oracl = new Conoracl ();
System.out.println (before "Operation:");
Oracl. Search ();
Oracl. ADD ();
Oracl. Delete ();
Oracl. Update ();
System.out.println ("After Operation:");
Oracl. Search ();
Oracl.close ();
}
}
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.