Java Connection Database

Source: Internet
Author: User

JDBC(java Data Base connectivity,java database connection) is a Java API for executing SQL statements , you can provide unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. is The standard specification for Java access to databases

JDBC is an interface, the driver is the implementation of the interface, no driver will not be able to complete the database connection, thus unable to operate the database! Each database vendor needs to provide its own driver to connect to its own company's database, which means that the driver is typically provided by the database generator vendor.

Connect database steps

1 1. Registration driver.2 tell the JVM which database driver to use3         //Drivermanager.registerdriver (New Driver ());4         //throw Com.mysql.jdbc.driver This implementation class into memory.5         //call static static code block after throwing into memory6         //registering drivers in static code blocks7Class.forName ("Com.mysql.jdbc.Driver");

1 2. Get the connection.2 use the classes in JDBC to complete the connection to the MySQL database3         //database name to connect to, write dead, encode the way? Characterencoding=utf-84String URL ="Jdbc:mysql://localhost:3306/goods?characterencoding=utf-8";5String user ="Root";6String Password ="1";7Connection conn =drivermanager.getconnection (URL, user, password);
1 3 . Get the statement execution platform 2         to get the performer object for an SQL statement by using a Connection object 3         Statement STA =conn.createstatement ();
4The Execute SQL statement uses the performer object to execute the SQL statement to the database to get the results after the execution of the database//Add Statement    /*String str = "INSERT into sort values (' 3 ', ' stationery ', ' pen ')";        int i =se.executeupdate (str); System.out.println (i);*/        //Modify Statement Update table name set the name of the table to be modified where condition/*String str = "Update sort set sdesc = ' Paike ' where sid = ' 1 '";        int i =se.executeupdate (str); System.out.println (i);*/        //DELETE statement    /*String str = "Delete from sort where sid = ' 7 '";        int i = se.executeupdate (str); System.out.println (i);*/        //Query StatementsString str ="select * from sort"; ResultSet Re=se.executequery (str);  while(Re.next ()) {System. out. println (Re.getint ("Sid") +re.getstring ("sname") +re.getstring ("Sdesc")); }
6 . Release resources. (first open back off)        Call a bunch of close () methods        rs.close ();        Sta.close ();        Conn.close (); */

Packaging focus

1 a new class is created here2 Package Com.orcale.demo;3 4 import java.sql.Connection;5 import Java.sql.DriverManager;6 import Java.sql.ResultSet;7 import java.sql.SQLException;8 import java.sql.Statement;9 Ten  Public classDemo04 { One     Private StaticConnection conn =NULL; Set as global variable A     Static{  -         Try {     -Class.forName ("Com.mysql.jdbc.Driver"); Register the program and then write the contents of the past parameters theString URL ="jdbc:mysql://localhost:3306/aaa";  -String user ="Root"; -String Password ="1"; -conn =drivermanager.getconnection (URL, user, password);  +}Catch(ClassNotFoundException |SQLException e) {Throws an exception - e.printstacktrace (); +         } A     } at         //Get Connection Object -          Public StaticConnection getconnection () { -             returnConn; -         } -         //Close the database -          Public Static voidClose (Connection conn,statement Stat,resultset re) throws sqlexception{ in             if(conn!=NULL){ - conn.close (); to             } +             if(stat!=NULL){ - conn.close (); the             } *             if(re!=NULL){ $ re.close ();Panax Notoginseng             } -         } the          Public Static voidClose (Connection Conn,resultset re) throws sqlexception{ +             if(conn!=NULL){ A conn.close (); the             } +             if(re!=NULL){ - re.close (); $             } $         } -}
1Connection conn =demo04.getconnection (); Call method gets2String sql ="SELECT * from user"; SQL statements3Statement stat =conn.createstatement (); Get Statement execution Platform4ResultSet re =stat.executequery (SQL); Called5          while(Re.next ()) {6System. out. println (Re.getstring ("name") +re.getstring ("Password"));7}


Java Connection Database

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.