Use of JDBC

Source: Internet
Author: User

Use of JDBC

Package com. jack. jdbc; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; // JDBC case public class JDBCTest {// use JDBC // first of all: the jar package/orcal is introduced in the app \ Administrator \ product \ 11.1.0 \ db_1 \ jdbc \ lib directory. The drivers for different databases are also different, you can search for the corresponding jar package public JDBCTest () {// TODO Auto-generated constructor stubtest ();} public st on the Internet. Atic void main (String [] args) {JDBCTest jdbc = new JDBCTest ();} public void test () {try {// 1. first load the driver // first find the Class where OrcalDriver is located. forName ("oracle. jdbc. driver. oracleDriver ");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // 2. configure the basic attribute String name = "scott"; // username String pass = "Ndbdta"; // password // connection statement, different databases have different connection statements: String url = "jdbc: oracle: thin: @ 127.0.0.1: 15 21: orcl "; // Connection type and host name Connection c = null; try {// use the driver to create a Connection c = DriverManager. getConnection (url, name, pass);} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // after the program has been connected to the database, // using PreparedStatement is much faster than statement. // PreparedStatement = null does not need to be precompiled; string SQL = ""; ResultSet rs = null; // you can directly modify try {SQL = "update emp set ename =? Where empno =? "; Statement = c. prepareStatement (SQL); // set? Replaced by the number of columns and type input value statement. setString (1, "jack"); statement. setInt (2, 7934); statement.exe cute ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // find try {SQL = "select * from emp "; statement = c. prepareStatement (SQL); rs = statement.exe cuteQuery ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} try {// before the result set first points to the first row, move to the first row while (rs. next () {int eno = rs. getInt (1); String ename = rs. getString (2); System. out. println (eno + "\ t" + ename) ;}} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.