Procedure for connecting ODBC to a database

Source: Internet
Author: User

Procedure for connecting ODBC to a database

We all know that databases are indispensable for any project. Common databases include MySQL, SQL Server, Oracle, and Access. Before operating databases, our programs, A step is to link to our database.

The procedure for connecting ODBC to a database is as follows:

1. Connect to ODBC through JDBC and obtain the connection object

(The imported package is omitted here)

...

Class. forName ("Driver Class Name ");

[Database-driven class names:

MySQL: com. mysql. jdbc. Driver

SQL Server: com. microsoft. jdbc. sqlServer. SQLServerDriver

Oracle: oracle. jdbc. Driver. OracleDriver

Access: sun. jdbc. JdbcOdbcDriver]

Connection conn = DriverManager. getConnection ("URL", "username", "password ");

If the database is accessed, the user name and password are not required.

2. Use the Statement interface to run SQL statements to manage data in our database

(The guide package code is still ignored here)

...

Statement stat = conn. createStatement ();

// If the query statement select * from table name where attribute =?

Stat.exe cuteQuery (SQL statement );

// If you add (insert into Table Name (attribute 1, attribute 2,...) values (?,?,...))

Delete (delete from table name where attribute 1 =? [And attribute 2 =?,...])

Modify Statement (Update table name set attribute 1 =? Where property 2 = ?)

Stat.exe cuteUpdate (SQL statement );

3. process the running results of SQL statements and perform corresponding operations according to the actual situation.

4. Close the database connection. [click the blackboard, because I often forget this step]

Stat. close ();

Conn. close ();

[As for this order, my habits are a bit like the features of stack queue, which are advanced and later. Of course, this is wrong. Different situations are different]

It is best to write these operations as a tool. After all, it is impossible for the project to write the above four steps every connection. Maybe my blog is not well written. If there is something wrong, I hope to give some advice, after all, I am still a little white, haha

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.