Connection acquisition of common data in JDBC 5

Source: Internet
Author: User

JDBC is seldom used directly in development (the persistence layer has many outstanding frameworks, such as Hibernate, MyBatis ...). ), but this is the basic knowledge that the Java programmer must be clear, here are some basic classes of knowledge, easy to use when reviewing later.

Java operations on the database are mainly the following steps

1) load driver, get database connection

2) Open transaction

3) Create statement

4) Perform operations in bulk or execute queries to get result sets, process results

5) Commit the transaction

6) Close the connection


First step: Load the driver class to get the database connection, provided the corresponding third-party jar package is available

1), SQL Server, need Sqljdbc4.jar

try {string username = "sa";//user name String password = "123";//Password Class.forName ("Com.microsoft.sqlserver.jdbc.SQLS Erverdriver ");//load driver class con = drivermanager.getconnection (" Jdbc:sqlserver://192.168.4.116:1433;databasename=  Unimondb ", username,password);//Get connection} catch (ClassNotFoundException e) {e.printstacktrace ();  } catch (SQLException e) {e.printstacktrace (); }

2) MySQL, need Mysql-connector-java-5.1.6-bin.jar

try {String username = "sa";   User name String password = "123456"; Password Class.forName ("Com.mysql.jdbc.Driver");//Load driver class Connection con = DriverManager. getconnection ("Jdbc:m   Ysql://192.168.4.116:55555/emm_manager ", username, password);//Get Connection System.out.println (con);  Con.close ();  } catch (ClassNotFoundException e) {e.printstacktrace ();  } catch (SQLException e) {e.printstacktrace (); }

3) PostgreSQL, need Postgresql-9.0-801.jdbc3.jar

try {String username = "sa";   User name String password = "123456"; Password Class.forName ("Org.postgresql.Driver");//Load driver class Connection con = DriverManager. getconnection ("Jdbc:p   Ostgresql://192.168.4.10:5432/unimondb ", username, password);//Get Connection System.out.println (con);  Con.close ();  } catch (ClassNotFoundException e) {e.printstacktrace ();  } catch (SQLException e) {e.printstacktrace (); }

4) Oracle, requires Ojdbc14.jar

try {String username = "sa";   User name String password = "123456";   Password Class.forName ("Oracle.jdbc.driver.OracleDriver"); Connection con = drivermanager.getconnection ("jdbc:oracle:thin:@192.168.0.39:1521:tarenadb", username, Passwo   RD);//Get Connection System.out.println (con);  Con.close ();  } catch (ClassNotFoundException e) {e.printstacktrace ();  } catch (SQLException e) {e.printstacktrace (); }

5) SQLite, need Sqlitejdbc-v033-nested.jar

try {class.forname ("Org.sqlite.JDBC");//load driver Connection con = drivermanager.getconnection ("jdbc:sqlite://d:/sq   Lite/learnsqlite.db ");//Get Connection System.out.println (con);  Con.close ();  } catch (ClassNotFoundException e) {e.printstacktrace ();  } catch (SQLException e) {e.printstacktrace (); }

Step two: Set up transaction commit

Con.setautocommit (false);//...... ..... Database operation (Increase and deletion) con.commit ();


About jar package, there is no download here, if you have a classmate to learn, you can private chat ...... .....


Connections to common data in JDBC 5 get

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.