JDBC Accesses Oracle Database example source code, including creating table, deleting table, inserting records, deleting records, querying records, etc.

Source: Internet
Author: User
Tags getmessage

Package Com.cb;public class Smsinfo {public static string ITEMINDEX = "Sms_index";p ublic static string itemto = "Sms_to";p Ublic static string itemfrom = "Sms_from";p ublic static string itemmsg = "Sms_msg";p ublic static string ITEMMSG2 = "Sms_ms G_binary "; int mindex;private string Mto;private string Mfrom;private string mmsg;private byte[] mmsg2;public SMSInfo (int Index, string to, string from, String msg, byte[] msg2) {mindex = Index;mto = To;mfrom = from;mmsg = MSG;MMSG2 = MSG2;} public int index () {return mindex;} Public String to () {return mTo;} Public String from () {return mfrom;} Public String msg () {return mmsg;} Public byte[] MSG2 () {return MMSG2;}}
Package Com.cb;import Java.sql.*;import Java.util.vector;import com.cb.cblogger.logger;public class OracleAdaptor{ private static String TAG = "Oracleadaptor";p rivate static Connection mconnection = null;private static Statement mstateme NT = null;private static PreparedStatement mpreparedstatement = null;private static ResultSet Mresultset = Null;private St  atic string TABLENAME = "Sms_to_submit";p rivate static string VARCHAR32 = "varchar";p rivate static string VARCHAR512 = varchar;p rivate static string integer = "INTEGER";p rivate static string blob = "blob"; static {Try{class.forname ("O Racle.jdbc.driver.OracleDriver ");} catch (Exception e) {LOGGER.W (TAG, "Initialize driver throw" + e.tostring ());}} Boolean getconnection () {String msg = "Connecting to"; String url = "jdbc:oracle:" + "thin:@192.168.1.106:1521:mybase"; orclmsg + = URL; String user = "C # #JACKY"; String password = "1234"; LOGGER.D (TAG, msg); Try{try{if (mconnection! = null) {mconnection.close (); mconnection = null;}} catch (SQlexception e) {LOGGER.W (TAG, E.getmessage ());} mconnection = drivermanager.getconnection (url, user, password); LOGGER.D (TAG, "Connected."); return true;} catch (SQLException e) {mconnection = null; LOGGER.W (TAG, "getconnection throw" + e.getmessage ());} return false;} private void Clearresource () {try{if (Mresultset! = null) {mresultset.close (); mresultset = null;}} catch (Exception e) {}try{if (mstatement! = null) {mstatement.close (); mstatement = null;}} catch (Exception e) {}try{if (mpreparedstatement! = null) {mpreparedstatement.close (); mpreparedstatement = null;}} catch (Exception e) {}try{if (mconnection! = null) {mconnection.close (); mconnection = null;}} catch (Exception e) {}}public Boolean createtable () {String createtablesql = "CREATE TABLE"; Createtablesql = Createtablesql + TABLENAME + "(" + Smsinfo.itemindex + "" + INTEGER + "," + Smsinfo.itemto + "" + VARCHAR32 + "," + Smsi Nfo. Itemfrom + "+ VARCHAR32 +", "+ smsinfo.itemmsg +" "+ VARCHAR512 +", "+ SMSINFO.ITEMMSG2 +" "+ BLOB +") "; try {i F (GETconnection ()) {mstatement = Mconnection.createstatement (); mstatement.executeupdate (Createtablesql);}} catch (SQLException ex) {LOGGER.W (TAG, "createtable throw" + ex.getmessage ()); return false;} Finally{clearresource ();} return true;} public Boolean droptable () {String droptablesql = "DROP TABLE" + tablename;try {if (getconnection ()) {mstatement = Mconnecti On.createstatement (); mstatement.executeupdate (Droptablesql);}} catch (SQLException ex) {LOGGER.W (TAG, "droptable throw" + ex.getmessage ()); return false;} Finally{clearresource ();} return true;}  public boolean insert (vector<smsinfo> SMSs, vector<smsinfo> submitedsmss) {String insertsql = ' INSERT INTO ' + TABLENAME + "(" + Smsinfo.itemindex + "," + Smsinfo.itemto + "," + Smsinfo.itemfrom + "," + Smsinfo.itemmsg + "," + Smsin Fo. ITEMMSG2 + ")" + "VALUES (?,?,?,?,?)"; try {if (!getconnection ()) {return false;} for (int i = 0; i < smss.size (); i++) {Smsinfo SMS = smss.get (i); mpreparedstatement = mconnection.preparestatement (insert SQL); Mpreparedstatement.setint (1, Sms.index ()); Mpreparedstatement.setstring (2, sms.to ());    Mpreparedstatement.setstring (3, Sms.from ()); Mpreparedstatement.setstring (4, sms.msg ()); Mpreparedstatement.setbytes (5, JSONADAPTOR.MDEFAULTMSG2); Mpreparedstatement.executeupdate (); Mpreparedstatement.close (); mpreparedstatement = Null;submitedsmss.add (SMS);}} catch (SQLException ex) {LOGGER.W (TAG, "insert throw" + ex.getmessage ());} Finally{clearresource ();} return true;} public boolean query (vector<smsinfo> SMSS) {String selectsql = ' select * from ' + tablename;try {if (!getconnection ()) {return false;}    Mstatement = Mconnection.createstatement ();    Mresultset = Mstatement.executequery (Selectsql); while (Mresultset.next ()) {//retrieve by column name int index = Mresultset.getint (Smsinfo.itemindex); String to = mresultset.getstring (SMSINFO.ITEMTO); String from = mresultset.getstring (Smsinfo.itemfrom); String msg = mresultset.getstring (smsinfo.itemmsg); byte[] msg2 = mresultset.getbytes (smsinfo.iteMMSG2);    Smsinfo SMS = new Smsinfo (index, to, from, MSG, MSG2); Smss.add (SMS); }} catch (SQLException ex) {LOGGER.W (TAG, "query throw" + ex.getmessage ());} Finally{clearresource ();} return true;}  public boolean Delete (Vector<integer> indexs) {String deletesql = ' Delete from ' + TABLENAME + ' WHERE '; for (int i = 0; I < indexs.size (); i++) {if (I! = 0) {Deletesql + = "or";} Deletesql + = (smsinfo.itemindex + "=" + indexs.get (i) + "'");} try {if (!getconnection ()) {return false;}    Mstatement = Mconnection.createstatement ();          Mresultset = Mstatement.executequery (Deletesql); Mstatement.executeupdate (deletesql);} catch (SQLException ex) {LOGGER.W (TAG, "delete throw" + ex.getmessage ());} Finally{clearresource ();} return true;} public void Accessoracle () {droptable (); CreateTable (); vector<smsinfo> tosubmit = new vector<smsinfo> (); vector<smsinfo> submited = new vector<smsinfo> (); for (int i = 0; i <; i++) {int index = i; String to = "1860000" + index; String FROm = "1880000" + index; String msg = "This message is sent to Alex, thank you." "; Tosubmit.add (new Smsinfo (index, to, from, MSG, JSONADAPTOR.MDEFAULTMSG2));} Insert (Tosubmit, submited); vector<smsinfo> quried = new vector<smsinfo> (); query (quried); vector<integer> indexs = new vector<integer> (); for (int i = 0; i < quried.size (); i++) {Indexs.add (new intege R (Quried.get (i). index ()));} Delete (Indexs); quried.clear (); query (quried);}    public static void Main (string[] args) {oracleadaptor dbTest = new Oracleadaptor (); Dbtest.accessoracle ();}}


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.