[JAVA100 example]053, loading JDBC Driver

Source: Internet
Author: User
Tags stmt create database oracle database

Import java.sql.*;


/**


* <P>TITLE:JDBC Connection Database </p>


* <p>description: This example demonstrates how to use JDBC to connect to an Oracle database and to demonstrate adding data and querying data. </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:JDBCConn.java</p>


* @version 1.0


*/


public class jdbcconn{


private String url= "";


private String username= "";


private String password= "";


/**


*<br> Method Description: Obtain data connection


*<br> input Parameters:


*<br> return Type: Connection Connection object


*/


public Connection Conn () {


try {


//load JDBC driver


class.forname ("Oracle.jdbc.driver.OracleDriver");


//CREATE DATABASE connection


Connection con = drivermanager.getconnection ("JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL", "Test", "test");


return con;


}catch (classnotfoundexception cnf) {


System.out.println ("Driver not find:" +cnf);


return null;


}catch (SQLException sqle) {


System.out.println ("Can´t Connection db:" +sqle);


return null;


} catch (Exception e) {


System.out.println ("Failed to load Jdbc/odbc driver.");


return null;


   }


 }


/**


*<br> Method Description: Execute query SQL statement


*<br> input parameters: Connection Con database connection


*<br> input parameter: String SQL SQL statement to execute


*<br> return type:


*/


public void query (Connection con, String sql) {


try{


if (con==null) {


throw new Exception ("Database connection can´t use!");


   }


if (sql==null) throw new Exception ("Check your parameter:´sql´! Don´t input null! ");


//Declaration Statements


Statement stmt = Con.createstatement ();


//Execute Query


ResultSet rs = stmt.executequery (SQL);


ResultSetMetaData Rmeta = Rs.getmetadata ();


//Get the number of data fields


int numcolumns = Rmeta.getcolumncount ();


while (Rs.next ())


 {


for (int i = 0;i< numcolumns;i++)


  {


String stemp = rs.getstring (i+1);


System.out.print (stemp+ "");


  }


System.out.println ("");


 }


}catch (Exception e) {


System.out.println ("Query error:" +e);


  }


 }


/**


*<br> Method Description: Perform inserts, updates, deletes, and so on without returning a result set of SQL statements


*<br> input parameters: Connection Con database connection


*<br> input parameter: String SQL SQL statement to execute


*<br> return type:


*/


public void execute (Connection con, String sql) {


try{


if (con==null) return;


Statement stmt = Con.createstatement ();


stmt.executeupdate (SQL);


}catch (Exception e) {


System.out.println ("Execute error:sql =" +sql);


System.out.println (e);


  }


 }


/**


*<br> Method Description: Example Demo


*<br> input Parameters:


*<br> return type:


*/


public void Demo () {


try{


jdbcconn oc = new Jdbcconn ();


Connection conn = Oc.conn ();


String sql = "INSERT into Tbl_user (Id,name,password) VALUES (Seq_user.nextval,´switch´,´haorenpingan´)";


Oc.execute (Conn,sql);


sql = "SELECT * from Tbl_user";


oc.query (Conn,sql);


Conn.close ();


}catch (SQLException se) {


System.out.println (SE);


}catch (Exception e) {


System.out.println (e);


  }





 }


/**


*<br> Method Description: Main method


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] arg) {


if (arg.length!=3) {


System.out.println ("Use:java jdbcconn URL username password");


return;


  }


jdbcconn oc = new Jdbcconn ();


oc.url = arg[0];


Oc.username=arg[1];


OC.PASSWORD=ARG[2];


Oc.demo ();


 }


}

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.