Beginners learn java----into JDBC

Source: Internet
Author: User
Tags throw exception

    • What is JDBC
    • Why use JDBC
    • How to use JDBC

What is JDBC?

JDBC (Java Data Base Connectivity, Java database connection) is a JAVAAPI for executing SQL statements that provides unified access to a variety of relational databases. In short, JDBC can do three things: establish a connection to the database, send an SQL statement, and process the results.

Why use JDBC?

Before JDBC, the application can manipulate the database through APIs and SQL statements provided by the database vendor, such as:

With JDBC, it's easy to send SQL statements to various relational data, in other words, with the JDBC API, you don't have to write a program specifically for the location MySQL database and write a program specifically for accessing the Oracle database. Or to access the SQL Server database and write another program, and so on, the programmer just need to write a program in the JDBC API, it can send SQL calls to the corresponding database, while the Java language and JDBC together so that programmers do not have to write different applications for different platforms, Simply write the program once to make it run on any platform. Such as:

How do you use JDBC?

The main functions of JDBC are as follows:

    • Establish a connection to a database or other data source
    • Sending SQL commands to the database
    • Processing the returned results of the database

Here is an example of a Java database using a program to question the entire process of the database.

Importjava.sql.*;//Import PackageImportJava.sql.Connection;ImportJava.sql.DriverManager; Public  class jdbcmain {    //Database links    PrivateConnection con =NULL;PrivateStatement st =NULL;//object that executes the SQL statement    PrivateResultSet rs =NULL;//Result set object     Public Jdbcmain() {//Bridge connection        Try{//Claims driverString Driver ="Com.microsoft.sqlserver.jdbc.SQLServerDriver";//Users is the name of the data sourceString ds ="Jdbc:sqlserver://localhost:1433;databasename=javaspecial";//The login name of the data sourceString user ="SA";//password for data sourceString Password ="123456";//LINK DatabaseClass.forName (driver);//Load the database drivercon = drivermanager.getconnection (ds, user, password);if(Con! =NULL) {System.out.println ("The database link was successful!" "); }        }Catch(Exception e) {System.out.println ("Database connection failed!" "+ e.tostring ()); }    }/** * Normal query * @param sqlquery * *     Public void Query(String sqlquery) {Try{//Connection con = null;Statement st = Con.createstatement ();//Get an example of statementResultSet rs = st.executequery (sqlquery);//Execute SQL statement, return result set            //When the returned result set is not empty, and there is still a record             while(rs! =NULL&& Rs.next ()) {intStu_num = Rs.getint (1);//Get the value of the 1th field in the current recordString name = Rs.getstring ("Name");//Get the value of the "Name" field in the current record                intAge = Rs.getint ("Age");//Get the value of the "age" field in the current record                intMath = Rs.getint ("Math");//Get the value of the "math" field in the current record                int中文版 = Rs.getint ("中文版");//Get the value of the "English" field in the current recordSystem.out.println ("Study number ="+ Stu_num +"\ t name ="+ name +"\ t age ="+ Age +"\ t Math score ="+ Math +"\ t english score ="+ 中文版); }        }Catch(Exception e) {//Throw exceptionSystem.out.println ("Error querying data"+ e.tostring ()); }    }/** */Add, delete, update */     Public void Add_update_del(String SQL) {Try{Statement st = con.createstatement ();//Generate statement Object            intx = st.executeupdate (SQL);//Perform actionsSystem.out.println ("Operation succeeded"+ x); }Catch(Exception e) {System.out.println ("Error in data modification"+ e.tostring ()); }    }//Main program     Public Static void Main(string[] args) {Jdbcmain JC =NewJdbcmain ();//EnquiryString name ="Zhang San"; String sqlquery ="SELECT * from Stu_info where name= '"+ name +"'"; Jc.query (sqlquery);//AddString Sqladd =INSERT INTO Stu_info values (7, ' Aberdeen ', 17,89,84) "; Jc.add_update_del (Sqladd);//ChangeString sqlupdate ="Update stu_info set math =95 where name = ' Zhang San '"; Jc.add_update_del (sqlupdate);//DeleteString Sqldelete ="Delete from Stu_info where Name= ' Zhang San '";    Jc.add_update_del (Sqldelete); }}

Here is a link to the source code download: JDBC Database connection test

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Beginners learn java----into JDBC

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.