Database operations are now the foundation of project development. To learn Java, you should first learn how to connect to the database. Using Java to connect to the database is not as simple as setting several attributes as using tools such as Delphi, complexity is actually complicated and troublesome. If you are a beginner, you cannot guarantee that the connection is successful for the first time. The following uses SQL Server 2000 as an example to describe the basic methods for connecting Java to the database, record your experiences.
1. Download SQL Server 2000 driver for JDBC
SQL Server 2000 Driver For JDBC Downloads
The driver has four versions so far. We recommend that you download the latest SP3 version.
After the driver is successfully installed, install the three drivers in the lib directory. jar files are added to CLASSPATH. If you are using JBuilder or Eclipse, add these three files to the project according to the IDE prompts.
2. Upgrade your SQL Server 2000 and install the latest patches.
This step may not be necessary because of the operating system environment. In the case of no patching, it can be connected normally, but sometimes it cannot, therefore, we recommend that you install the latest SQL Server 2000 patch (SP4) and JDBC driver (SP3 ).
If your program prompts "Error establishing socket" during running, you can fix it by patching SQL Server 2000.
3. Driver loading method
Before establishing a connection, load the SQL Server 2000 JDBC driver. The code format is as follows:
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");
Note that the parameter string of the forName method must be exactly the same as the preceding content. The case sensitivity is case sensitive. In fact, this string is the complete name of the driver class: Package name + class name.
4. Get a connection
Before operating the database, you must first obtain a connection to the database, using the following code format:
DriverManager. getConnection (connection string, logon user name, logon password );
Example:
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, which can be changed; the localhost part is the port number used by SQL Server, which can be modified according to the actual situation; databaseName is the name of the database to be connected. Note that the database name before DatabaseName is a semicolon rather than a colon.
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.