Example 3 jar package connection sqlserver2000-msbase. jar; MSSQLServer. jar; msutil. Jar

Source: Internet
Author: User
Tags mssqlserver


Example 3 jar package connection sqlserver2000-msbase. jar; MSSQLServer. jar; msutil. Jar


1. Ensure that the environment is OK. 1. Install JDK. 2. Download The JDBC driver.

Here msbase. jar; MSSQLServer. jar; download msutil. jar: http://download.csdn.net/source/2474798

(Sorry, I forgot to change the resource score to 0 during the upload process. I cannot re-upload the resource directly)


2. Establish a working environment:


Briefly describe the role of each file, according to the creation order:

1、sqlserver_driver_lib.rar: The downloaded JDBC driver;

2. msbase. jar; MSSQLServer. jar; msutil. jar: The decompressed jar package;

3. Create an external document. txt: used to compile Java code and BAT files;

4. testsqlserver2000.java: Java source code. The content is as follows:

Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. statement; <br/> Import Java. SQL. *; <br/> public class testsqlserver2000 <br/> {<br/> // driver string <br/> static string jdbcdriver = "com. microsoft. JDBC. sqlserver. sqlserverdriver "; <br/> // connection string <br/> string jdbcconnection =" JDBC: Microsoft: sqlserver: // data: 1433; databasename = Master "; <br/> string username =" SA "; <br/> string userpwd =" sa "; <br/> connection conn = NULL; <br/> resultset rs = NULL; </P> <p> Public testsqlserver2000 () <br/>{< br/> try {<br/> class. forname (jdbcdriver); // load driver <br/>}< br/> catch (Java. lang. classnotfoundexception e) <br/>{< br/> system. err. println ("dbconnection ():" + E. getmessage (); // catch exception <br/>}</P> <p> Public connection connect () <br/>{< br/> try {// G Et connection <br/> conn = drivermanager. getconnection (jdbcconnection, username, userpwd); <br/> system. out. println ("Connect successful"); <br/>}< br/> catch (sqlexception ex) <br/>{< br/> system. err. println ("connection ():" + ex. getmessage (); <br/>}< br/> return conn; <br/>}</P> <p> Public void close () <br/>{< br/> try {<br/> If (! This. conn. isclosed () <br/>{< br/> This. conn. close (); <br/> system. out. println ("Close successful"); <br/>}</P> <p>} catch (sqlexception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> testsqlserver2000 CDB = new testsqlserver2000 (); <br/> CDB. connect (); <br/> statement stmt; <br/> resultset RS; <br/> try {<br/> stmt = CDB. conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable); <br/> rs = stmt.exe cutequery ("select * From spt_monitor"); <br/> while (RS. next () <br/>{< br/> string S = Rs. getstring (1); <br/> system. out. println (s); <br/>}< br/>} catch (sqlexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> CDB. close (); <br/>}</P> <p>

5. Use the javac. BAT file to compile the Java file. The content of the file is as follows. Double-click the file and run it to get testsqlserver2000.class.

Set Path = "C:/program files/Java/jdk1.6.0 _ 11/bin; C:/windows; C:/Windows/command" <br/> set classpath = ".; c:/program files/Java/jdk1.6.0 _ 11/lib/tools. jar "<br/>" C:/program files/Java/jdk1.6.0 _ 11/bin/javac "testsqlserver2000.java <br/> pause <br/>

Execution result:

I:/workspace/Java/testsqlserver2000> set Path = "C:/program files/Java/jdk1.6.0 _ 11/<br/> bin; C:/windows; C: /Windows/command "</P> <p> I:/workspace/Java/testsqlserver2000> set classpath = ".; c:/program files/Java/jdk1 <br/>. 6.0 _ 11/lib/tools. jar "</P> <p> I:/workspace/Java/testsqlserver2000>" C: /program files/Java/jdk1.6.0 _ 11/bin/javac <br/> "testsqlserver2000.java </P> <p> I: /workspace/Java/testsqlserver2000> pause <br/> press any key to continue...

6. testsqlserver2000.class is the bytecode file compiled by Java. The file actually parsed by the Virtual Machine

7. java. Bat calls Java to execute testsqlserver2000, and its code is:

@ Set Path = "C:/program files/Java/jdk1.6.0 _ 11/bin; C:/windows; C: /Windows/command "<br/> @ set classpath = ".; c:/program files/Java/jdk1.6.0 _ 11/lib/tools. jar; MSSQLServer. jar; msbase. jar; msutil. jar "<br/>" C:/program files/Java/jdk1.6.0 _ 11/bin/Java "testsqlserver2000 <br/> pause <br/>

Double-click execution result

I:/workspace/Java/testsqlserver2000> "C: /program files/Java/jdk1.6.0 _ 11/bin/Java "<br/> testsqlserver2000 <br/> connect successful <br/> 01:30:31. 687 <br/> close successful </P> <p> I:/workspace/Java/testsqlserver2000> pause <br/> press any key to continue...

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.