JDBC connection to SQL Server

Source: Internet
Author: User

Class. forname ("com. microsoft. JDBC. sqlserver. sqlserverdriver "). newinstance (); <br/> string url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = mydb"; <br/> string user = "sa "; <br/> string Password = ""; <br/> connection conn = drivermanager. getconnection (URL, user, password); <br/> statement stmt = Conn. createstatement (); <br/> resultset rs = stmt.exe cutequery (SQL); <br/> while (RS. next () <br/>{< br/> system. out. println (RS. getstring (1); // print the first column of the table <br/>}

 

 

Java JDBC programming Summary

Article category: Java programming

Hibernate, toplink, etc. or
The technology of ing database operations is based on the JDBC technology. In practice, their performance is very different from that of JDBC. However, if JDBC is not used well, it would be worse.
What about hibernate. For the time being, I will not mention the advantages and disadvantages of Java. I will summarize the basic technologies of Java to deepen my understanding.

I. Basic principles of JDBC

JDBC is the Technical Specification for Java database operations. It actually defines a set of standard database operation interfaces. To allow Java to operate databases, JDBC must be implemented.
Interface Class. Different database vendors provide JDBC interface implementation to allow the Java language to operate their own databases. These classes that implement the JDBC interface are packaged into a jar package, that is
We usually see the database driver. Because different databases have different data operation mechanisms, the specific implementation of JDBC varies widely. However, as a Java programmer
The JDBC interface is handed in, so you don't have to worry about how they implement it! Now I know what the JDBC driver is. Of course, these classes can be written by yourself-if you are awesome!

Ii. JDBC programming steps

To illustrate this step, assume that you want to execute an SQL query using a Java program, you need to follow the steps below:

1. Create the URL of the specified database

This URL is actually a unified resource locator, which contains some information about connecting to the database: database type, port, driver type, connection method, IP address (or name) of the database server) database Name (some are aliases. The format is as follows:

JDBC: subprotocol: subname: // dbserverip: Port/databasename

For example, a MySQL URL: JDBC: mysql: // 192.168.3.143: 3306/zfvims

2. Load the driver class to the JVM memory area.

There are two methods:

One is to use the class. forname () method to load the specified driver.

One is to add the driver to the JDBC. Drivers attribute of Java. Lang. system.

The last note is that sometimes the classpath environment variable added to the system does not work because the JVM has not loaded the driver before using the JDBC interface to operate the database.

Class. forname ("com. MySQL. JDBC. Driver ")

3. Use the drivermanager class to manage drivers and create database connections.

The drivermanager class acts between programmers and JDBC drivers and provides methods such as registering management drivers to establish connection details. All its members are static. The getconnection method creates a JDBC connection object.

Connection conn = drivermanager. registerdriver ("JDBC: mysql: // 192.168.3.143: 3306/zfvims", "lavasoft", "password ");

4. Connection class-database connection

The connection class mentioned here is actually a class that implements the JDBC connection interface. This class is generally implemented by the JDBC driver.
The connection class indicates a database connection. You can obtain detailed information about database objects such as databases and tables through its objects. But more is to send data to the database through this connection.
Run the SQL statement.

Database Connection establishment is resource-and time-consuming. Therefore, you must close the connection through the close () method to release system resources without connection.

5. Statement class-send and execute (static) SQL statements

You can create a statement object by using the createstatement () method of the connection object. You can (send) and execute
A static SQL statement. If you want to execute a dynamic SQL statement (with parameters in the SQL string), use the preparedstatement class. Its usage is similar to statement.

Statement stmt = con. createstatement ()

6. resultset class -- result set

After you execute an SQL query, a query result is generated. Resultset indicates the data table of the database result set. It is usually generated by executing the statement to query the database.
The resultset object has a pointer to its current data row. You can use the resultset object to obtain not only the result set data, but also the column name and Data Type of the result set table.

Resultset rs1_stmt.exe cutequery (SQL)

7. Close database connection

After the SQL operation is completed, you should close the database connection to avoid consuming system resources because the connection is not closed. if the system does not close each time, multiple connections will be established for multiple operations, eventually, the database connection will reach the maximum limit, or the system resources will be exhausted, resulting in application crash. Therefore, pay attention to disabling resources, especially database connections.

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.