Java Connection SQL Server 2000

Source: Internet
Author: User
Tags add connect sql server driver sql socket string version port number
Server

Database operation is now the fundamental development of the project, learning Java should first learn how to connect the database, with the Java connection database is not like the use of Delphi tools such as a few attributes on OK, said simple also simple, said complex, in fact, very complex, and very troublesome, if it is a beginner, There is no guarantee that the connection will be successful for the first time, as an example of SQL Server 2000, the basic method of Java connecting to a database is also noted.

1. Download SQL Server driver for JDBC
SQL Server Driver for JDBC Downloads
The driver is currently available in four versions and recommends downloading the latest SP3 version.
After the driver installation is successful, add the three. jar files in the Lib directory under the installation directory to Classpath, and if you are using JBuilder or eclipse, add the three files to the project according to the IDE's prompts.

2, upgrade your SQL Server 2000, and make it the latest patches.
This step may not be required, depending on the operating system environment, in the case of not patching, sometimes normal connection, sometimes not, so it is recommended to install the latest SQL Server 2000 patch (SP4) and JDBC Driver (SP3).
If your program prompts at run time: Error establishing socket, in general, a patch of SQL Server 2000 can be resolved.

3, the driving load method
Before establishing a connection, load the SQL Server version JDBC driver first, as follows:
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver");
Note that the parameter string for the Forname method must be exactly the same as the above, the case is differentiated, in fact this string is the full name of the driver class: Package name + class name.

4. Get a connection
Before you manipulate the database, get a connection to the database, using the following code format:
Drivermanager.getconnection (connection string, login username, login password);
Cases:
Drivermanager.getconnection ("jdbc:microsoft:sqlserver://localhost:1433; Databasename=pubs "," sa "," "");
The key here is the content of the connection string, the localhost part is the name of the server, you can change it; 1433 is divided into the port number used by SQL Server, modified according to the actual situation; DatabaseName is the name of the database to which you want to connect. Note that before DatabaseName is a semicolon, not a colon.

5. code example

Import Java SQL package, required to connect to database;
Import java.sql.*;

public class TestDB {
public static void Main (string[] args) {
String drivername = "Com.microsoft.jdbc.sqlserver.SQLServerDriver";
String Dburl = "jdbc:microsoft:sqlserver://localhost:1433;" Databasename=pubs ";
String userName = "sa";
String userpwd = "";
Connection Dbconn

try {
Class.forName (drivername);
Dbconn = Drivermanager.getconnection (Dburl, UserName, userpwd);
System.out.println ("Connection successful!");
}
Catch (Exception e) {
E.printstacktrace ();
}
}
}

6. Problems that may arise
If the above code is run, output "Connection successful!", that means that everything is normal, connect the database successfully, you can carry out statement, resultset operation, on the contrary, there must be a corresponding anomaly.
If you are prompted with an error establishing socket, install the appropriate SQL Server 2000 patch as described earlier.
If the hint "classnotfoundexception", that must be Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"); This piece of code is spelled incorrectly, or the three. jar files in the SQL Server Driver for JDBC Lib directory are not joined to the classpath.

< end >




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.