Database operation is now the foundation of project development. When learning Java, you should first learn how to connect to the database. Connecting to the database with Java is not like using tools such as Delphi to set a few properties. It may be said to be simple or complex, but it is actually quite complicated. , and very troublesome. If you are a beginner, there is no guarantee that the connection will be successful the first time. Let's take SQL Server 2000 as an example to talk about the basic method of connecting to the database in Java, and also record your experience.
1. Download SQL Server 2000 driver for JDBC
SQL Server 2000 Driver For JDBC Downloads
There are currently four versions of this driver, and it is recommended to download the latest SP3 version.
After the driver is installed successfully, please add the three .jar files in the lib directory in the installation directory to CLASSPATH; if you are using JBuilder or Eclipse, you can also add these three files to the project according to the IDE prompts. .
2. Upgrade your SQL Server 2000 and apply the latest patches.
This step may not be necessary. Depending on the operating system environment, sometimes the connection can be normal without patching, and sometimes not, so it is recommended to install the latest SQL Server 2000 patch (SP4) and JDBC driver (SP3).
If your program prompts: Error establishing socket when running, usually it can be solved by applying a SQL Server 2000 patch.
3. Driver loading method: Before establishing a connection, the SQL Server 2000 JDBC driver must be loaded first. The code form is as follows:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Note here that the parameter string of the forName method must be exactly the same as the above content, and it is case-sensitive. In fact, this string is the complete name of the driver class: package name + class name.
4. Obtain a connection. Before operating the database, you must first obtain a connection to the database. Use the following code format:
DriverManager.getConnection(connection string, login username, login 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 and can be changed; the 1433 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 DatabaseName is preceded by a semicolon, not a colon.
5. Code examples
//Import the Java SQL package, required to connect to the 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. Possible problems. If "Connection Successful!" is output after the above code is run, it means that everything is normal, the database connection is successful, and you can perform Statement and ResultSet operations; otherwise, a corresponding exception must have occurred. .
If the error "Error establishing socket" is prompted, please install the corresponding SQL Server 2000 patch according to the previous instructions.
If "ClassNotFoundException" is prompted, it must be Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); the code is spelled incorrectly, or it is the three .jar files in the SQL Server 2000 Driver For JDBC Lib directory. Not added to CLASSPATH.
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/panwenju/archive/2009/12/28/5089049.aspx