Java Database connection--jdbc-odbc Bridge connection mode

Source: Internet
Author: User
Tags odbc

JDBC-ODBC Bridge Connection mode operation database SU (Course)

Steps:

1. Configure the data source

Control Panel Search Administration Tool->ODBC Data Source (32-bit), add, select SQL Server (fill in name mytest, Server local or.) Next, change the default database to su-> Next, test data source to success

The user data source will have one more mytest to configure the data source successfully.

2. Connect to the data source in the program

Open Eclipse and write the program.

1  Public classDemo_1 {2 3      Public Static voidMain (string[] args) {4Connection ct=NULL;5Statement sm=NULL;6         7         Try {8             //1. Load Driver (function: Add the required driver to the memory)9Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");Ten              One             //2. Get Connected (specify which data source to connect to) A             //If you select Windows NT Authentication when you configure a data source, you do not need a user name and password -             //If you select user authentication, you need to add a username and password -Ct=drivermanager.getconnection ("Jdbc:odbc:mytest"); the          -             //3. Create statement or preparedstatement[differences] -Sm=ct.createstatement ();//statement use: used primarily to send SQL statements to a database -              +             //4. Execute (crud, create DATABASE, back up database, delete data ...) -             //Demo Adds a piece of data to the course table, executeupdate can perform add delete and modify operations +             intI=sm.executeupdate ("INSERT into Course values (' 8 ', ' Software test ', 4, 3)"); A              at             if(i==1) { -System.out.print ("Add Success"); -}Else { -System.out.print ("Add unsuccessful"); -             } -              in}Catch(Exception e) { - e.printstacktrace (); to}finally{ +             //Close Resources!!! The order is who is created after the first close -             Try { the                 if(sm!=NULL){//The IF statement is for the robustness of the program * sm.close (); $                 }Panax Notoginseng                 if(ct!=NULL){ - ct.close (); the                 }         +}Catch(SQLException e) { A e.printstacktrace (); the             }         +         } -     } $}

Executes the program and the console output is added successfully.

Open SQL server2012 to view the course table of the SU database, and indeed one more data, the operation succeeds.

Java Database connection--jdbc-odbc Bridge connection mode

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.