Java database connection technology-JDBC and java database connection jdbc

Source: Internet
Author: User

Java database connection technology-JDBC and java database connection jdbc

Hello everyone, today we learned how to connect Java to the database. I have learned about database operations in the. net language before, and I feel like they are all the same.

JDBC is short for Java database connection technology and provides the ability to connect to various common databases.

Jdbc api (main function: establish a connection with the database, execute statements, and process results ):

Provider: Sun

Content: interfaces and classes for programmers to call are integrated in java. SQL and javax. SQL (the following x is extend, which means extension ). For example:

DriverManager class (managing different JDBC drivers ),

Connection interface (connecting data and transmitting data ),

Statement interface (Execute SQL statements ),

ResultSet interface (processing results based on the result set returned by statement ).

Procedure:

1. Load the JDBC driver;

2. Create a database connection object;

3. Create the command object statement to execute the SQL statement and return the result set;

4. ResultSet processes the result through the returned result set;

5. Close the database connection and release resources.

Here is an example of user login verification:

  

Package jdbc; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import java. util. connector;/*** connect to the database through JDBC. java * 11:14:48 * John */public class Test {public static void main (String [] args) {empty input = new empty (System. in); System. out. println ("Enter the User name:"); String username = input. next (); System. out. pr Intln ("Enter Password:"); String userpwd = input. next (); // 1. load the JDBC driver try {Class. forName ("com. mysql. jdbc. driver "); // MySQL drive} catch (ClassNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} // 2. create a database Connection object Connection conn = null; Statement stat = null; ResultSet rs = null; try {// mysql Connection URL format: // jdbc: mysql: // host name: Connection port/Database Name String url = "jdbc: mysql: // localhost: 3306/Pet"; conn = Driv ErManager. getConnection (url, "root", "123456"); // 3. CREATE command object statement stat = conn. createStatement (); // 4. run the command and return the result set String SQL = "select count (1) as cnt from users where UserName = '"+ username +"' and userpwd = '"+ userpwd +"' "; rs1_stat.exe cuteQuery (SQL); while (rs. next () {int count = rs. getInt ("cnt"); if (count = 1) {System. out. println ("Logon successful! ");} Else {System. out. println (" Logon Failed! ") ;}} Catch (SQLException e) {// remember to handle the exception // TODO Auto-generated catch block e. printStackTrace ();} finally {// 5. close database connection try {conn. close (); stat. close (); rs = null;} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}

 

The above is my personal opinion. If you have any objection, I hope you can correct it and learn and make progress together!

  

  

 

 

 

 

  

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.