Java uses ODBC to connect to the database

Source: Internet
Author: User
Microsoft SQL Server 2000 SP4 Simplified Chinese version: Http://www.crsky.com/soft/5814.html SQL Server 2000 driver for JDBC: Http://www.microsoft.com/downloads/results.aspx? Displaylang = en & freetext = SQL + SERVER + 2000 + driver + for + JDBC # 1. Download SQL Server 2000 driver for JDBC. The driver has four versions up to now, it is recommended to download the latest SP3 version, SQL Server 2000 driver for JDBC Service Pack 3: http://www.microsoft.com/downloads/details.aspx? Familyid = 07287b11-0502-461a-b138-2aa54bfdc03a & displaylang = en # filelist after the driver is successfully installed, please install three in the lib directory under the installation 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. port 1433 ProblemsIn SP2, the firewall disables this port by default. Open it by yourself. Control Panel-firewall-exception, add TCP 1433 port UDP 1433 3. 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. 4. right-click the project name, select "Java build path", click "Database", select "add external", and select the path "system drive letter: /program files/Microsoft SQL Server 2000 driver for JDBC/lib. jar, MSSQLServer. jar, msutil. jar. 5. Driver Loading MethodBefore 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. 6. Get a connectionBefore operating the database, you must first obtain a connection to the database, using the following code format: drivermanager. getconnection (connection string, login user name, 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, which can be changed; the part 1433 is the port number used by SQL Server, which can be changed according to the actual situation; the databasename is the name of the database to be connected, note that databasename is preceded by a semicolon instead of a colon. 7. code examplePackage smxc; import Java. SQL. *; public class SQL _conn {public static void main (string [] ARGs) {string drivername = "com. microsoft. JDBC. sqlserver. sqlserverdriver "; string dburl =" JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = smxc "; // smxc database name string username =" sa "; string userpwd = "sa"; connection SQL _conn; try {class. forname (drivername); SQL _conn = drivermanager. getconnection (dbu RL, username, userpwd); system. Out. println ("connection successful! ");} Catch (exception e) {e. printstacktrace ();}}} 1. oracle8/8i/9i Database (thin Mode) Class. forname ("oracle. JDBC. driver. oracledriver "). newinstance (); string url = "JDBC: oracle: thin: @ localhost: 1521: orcl"; // orcl indicates SidString user = "test"; string Password = "test"; connection conn = drivermanager. getconnection (URL, user, password ); 2. MySQL database Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance (); string url = "JDBC: mysql: // localhost/mydb? User = soft & Password = soft1234 & useunicode = true & characterencoding = 8859_1 "// mydb indicates the Database Name Connection conn = drivermanager. getconnection (URL );
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.