JDBC Application advanced article in servlet (i)

Source: Internet
Author: User
Tags mssql server port number
JDBC uses the database URL to describe the database driver. The database URL is similar to a generic URL, but sun simplifies the definition with the following syntax:

Jdbc: : [Node]/[database]

Where the Child Protocol (subprotocal) defines the driver type, node provides the location and port number of the network database, followed by an optional parameter. For example:

String url= "Jdbc:inetdae:myserver:1433?language=us-english&sql7=true"

Represents the use of the Inetdae driver to connect to the MyServer database server on port 1433, select the language as U.S. English, and the version of the database is MSSQL Server 7.0.

The Java application loads a driver class by specifying DriverManager. The syntax is as follows:

Class.forName (" ”);

Or

Class.forName (" "). newinstance ();

Then, DriverManager creates a specific connection:

Connection connection=drivermanager.getconnection (Url,login,password);

The connection interface connects the database by specifying the database location, login name, and password. Connection interface creates a statement real

Example executes the query you want:

Statement stmt=connection.createstatement ();

Statement has a variety of methods (APIs), such as Executequery,execute, that can return a query's result set. The result set is a ResultSet object. Specific can be developed through the JDBC document view. can be downloaded at Sun's site

The following example illustrates:

Import java.sql.*; Enter JDBC Package

String url = "jdbc:inetdae:myserver:1433";//host name and port

String login = "user";//Login name

String password = "";//Password

try {

Drivermanager.setlogstream (System.out); file://open a stream for displaying some information

FILE://calls the driver, whose name is Com.inet.tds.TdsDriver

File://Class.forName ("Com.inet.tds.TdsDriver");

file://Setting timeout

Drivermanager.setlogintimeout (10);

file://Open a connection

Connection Connection = drivermanager.getconnection (Url,login,password);

file://get the database driver version

DatabaseMetaData CONMD = Connection.getmetadata ();

System.out.println ("Driver name:\t" + conmd.getdrivername ());

System.out.println ("Driver version:\t" + conmd.getdriverversion ());

FILE://Select Database
Connection.setcatalog ("MyDatabase");

FILE://Create statement


Statement st = Connection.createstatement ();

file://Execute Query

ResultSet rs = st.executequery ("SELECT * from MyTable");

file://get results, output to screen

while (Rs.next ()) {

for (int j=1; J<=rs.getmetadata (). getColumnCount (); j + +) {

System.out.print (Rs.getobject (j) + "T");

}

System.out.println ();

}

file://Close Object

St.close ();

Connection.close ();

catch (Exception e) {

E.printstacktrace ();

}

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.