Java Operations Oracle Build tables, update records

Source: Internet
Author: User

1. Creating a Table Class

Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.sql.PreparedStatement;
public class lx01{

public static void Main (string[] args) throws SQLException, classnotfoundexception{
Load Driver
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Java.sql.DriverManager.registerDriver (New Oracle.jdbc.driver.OracleDriver ());

Establish a connection
String url = "Jdbc:oracle:thin:@100.100.100.20:1521:dbsid2";
String user= "Scott";
String password= "Tiger";
Connection con=java.sql.drivermanager.getconnection (URL, user, password);

EXECUTE statement
String createtablecoffees = "CREATE TABLE coffees" +
(Cof_name VARCHAR (+), sup_id INTEGER, Price FLOAT, "+
"SALES integer, Total integer)";
String createtablesup = "CREATE TABLE support (sup_id integer, Sup_name varchar (+), City varchar (20))";
String insertcoff= "INSERT into coffees values (' Colombian ', 101,7.99,10,50)";
String insertsupp= "INSERT into support values (101, ' Acme,inc. ', ' new.york ')";
Statement smt=con.createstatement ();
int rs=smt.executeupdate (INSERTSUPP);
}

}

2. Updating the class for table records

Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.sql.PreparedStatement;
public class lx03{


public static void Main (string[] args) throws SQLException, classnotfoundexception{
Load Driver
Oracledirver ();
Establish a connection
Connection Con=connect ();
Statement smt=con.createstatement ();
/* string[] sqlchar ={"INSERT into coffees values (' Saber tee ', 101,8.99,12,50) '," INSERT into coffees values (' CLA RK Tee ', 102,9.99,22,50) "," INSERT into support values (102, ' Inspur,inc. ', ' Beijing ') "};
for (int i=0;i<3;i++) {
Smt.executeupdate (Sqlchar[i]);
}
*/
String sql= "Select B.sup_name, a.cof_name,a.price*a.sales from coffees A,support b where b.sup_id=a.sup_id";
ResultSet rs=smt.executequery (SQL);
while (Rs.next ()) {
System.out.println (rs.getstring (1) + "" "+rs.getstring (2) +" "+rs.getstring (3));
}
}
Load Driver
public static void Oracledirver () throws SQLException, classnotfoundexception{
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Java.sql.DriverManager.registerDriver (New Oracle.jdbc.driver.OracleDriver ());
}
Establish a connection
public static Connection Connect () throws SQLException, classnotfoundexception{
String url = "Jdbc:oracle:thin:@100.100.100.20:1521:dbsid2";
String user= "Scott";
String password= "Tiger";
Connection con=java.sql.drivermanager.getconnection (URL, user, password);
return con;
}
/* public static string[] SQL () {
string[] Sqlchar = null;
sqlchar[0]= "INSERT into coffees values (' Saber tee ', 101,8.99,12,50)";
sqlchar[1]= "INSERT into coffees values (' Clark tee ', 102,9.99,22,50)";
sqlchar[2]= "INSERT into support values (102, ' Inspur,inc. ', ' Beijing ')";
return SQLCHAR;
}
*/
}

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.