Test ODBC connection MySQL database

Source: Internet
Author: User
Tags odbc connection

The following example is the simplest example of an ODBC connection to a MySQL database.

General steps:

1. Registration drive;

2. Establish a connection;

3. Create a statement;

4. Processing the results;

5. Release resources.

Attention:

1. Software development environment: MyEclipse 8.5+ MySQL5.1

2. The MySQL driver package needs to be loaded before the code is run;

Database information:

The source code is as follows:

Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;/*********************************** * Test ODBC connection mysql database * MySQL database * login account: Root * Login Password: Toor * * Build database name: TESTODBC * Table name : T_users * Field: * ID int * username VARCHAR 255 * Password VARCHAR 255 * ********** ************************/ Public classOdbc_conn_mysql { Public Static voidMain (string[] args)throwsSQLException {//1. Registration DriverDrivermanager.registerdriver (Newcom.mysql.jdbc.Driver ()); //2. Establish a connectionConnection conn =Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/testodbc", "root", "Toor"); //Create statementStatement st =conn.createstatement (); //EXECUTE StatementResultSet rs = st.executequery ("SELECT * from T_users"); //Processing Results         while(Rs.next ()) {//Read and Save database query results            intx = Rs.getint ("ID"); String S1= Rs.getstring ("username"); String S2= rs.getstring ("Password"); //Print ResultsSystem.out.println ("id=" +x); System.out.println ("Username=" +S1); System.out.println ("Password=" +S2); }                //Freeing ResourcesRs.close ();        St.close ();    Conn.close (); }}

Run:

Test ODBC connection MySQL database

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.