MySQL---JDBC

Source: Internet
Author: User

Java Database Connector Step:

(Eclipse)

You can import the jar package by right-clicking on the project item, tapping the properties below, and then following the sequence of keywords.

Key points: 1. Java Build Path

2.Libraries

3.ADD External JARs (add local JARs pack)

4.Apply

JDBC Common interfaces, classes:

1. Database-driven

Driver interface and DriverManager class

2. Database connection

Connection class

3. Execute the SQL text

Statement class

PreparedStatement class (can contain placeholders)

Code reference:

 PackageCom.zczr.ja01;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.sql.Connection; Public classjdbc_test01 { Public Static voidMain (string[] args) {Connection con=NULL; Statement Stat=NULL; ResultSet RS=NULL ; //Jdbc-java Database Connector                Try {            //load the MySQL driver and load the MySQL driver into the Drivermanage classClass.forName ("Com.mysql.jdbc.Driver"); //Connection AddressString url = "Jdbc:mysql://127.0.0.1:3306/goods"; //User nameString userName = "root"; //User PasswordString password = "1234"; //The database link address, database user name, password to be passed by the driver administrator to obtain the connection object (Connection) of the databaseCon =drivermanager.getconnection (Url,username,password); //Writing SQL statementsString sql = "SELECT * FROM Goodsinfo"; //get the execution object for SQLStat =con.createstatement (); /*** Use SQL Execution object to execute an already written SQL statement * and return an execution result set (ResultSet)*/RS=stat.executequery (SQL);  while(Rs.next ()) {intid = rs.getint ("GID"); String name= Rs.getstring ("Gname"); String type= Rs.getstring ("Gtype"); DoublePrice = Rs.getdouble ("Price"); intnum = rs.getint ("num"); intOrder = Rs.getint ("oder"); String SR= ID + "+ name +" "+" "+ Price +" "+ num +" "+ order +" \ n "; System.out.println ("Query result is: \ n" +SR); }        } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{            Try {                if(rs! =NULL) {rs.close (); }                if(Stat! =NULL) {rs.close (); }                if(Con! =NULL) {rs.close (); }            }Catch(SQLException e) {e.printstacktrace (); }        }    }        }

MySQL---JDBC

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.