Java Connection SqlServer2008 Database

Source: Internet
Author: User
Tags microsoft sql server

Java Connection SqlServer2008 Database

First download jdbc::http://www.microsoft.com/zh-cn/download/details.aspx?id=21599

After the download is complete, it is an EXE file, click Run, you will be prompted to select the Unzip directory.

After the decompression is complete, enter < You extract to the directory >\sqljdbc_3.0\chs, there are two things we need

One is: Sqljdbc.jar, the other is Sqljdbc4.jar.

Use Sqljdbc4.jar here

First Configure SA authentication:

Since installing sqlServer2008 is installed with Windows authentication and no SQL Server identity user is added for sqlServer2008, users are added first:

Open Microsoft SQL Server managerment Studio and log on as Windows authenticated, on the left, Object Explorer, security-login, right-click the Sa-> property, add a password for the SA user, Select SQL Server Authentication, and in the Status option, grant connect to the database and login enabled. Right-click the root node of the Object Explorer, select Properties, Security->sqlserver, and Windows Authentication mode, so that SQL Server 2008 the user SA was created with SQL Server authentication.

There are two ways to connect the sqlserver2008 database in Java code, one is the SA authentication mode and the other is the hybrid authentication mode:

First: SA authentication mode, with the URL of the Java code below

    Importjava.sql.Connection; ImportJava.sql.DriverManager; ImportJava.sql.ResultSet; Importjava.sql.Statement;  Public classTest { Public Static voidMain (String args[]) {//Create A variable for the connection string. String Connectionurl = "jdbc:sqlserver://localhost:1433;" + "databasename=adventureworks;integratedsecurity=true;"; String URL= "Jdbc:sqlserver://127.0.0.1:1368;databasename=mydb;user=sa;password=qiaoning";//SA identity ConnectionString url2= "jdbc:sqlserver://127.0.0.1:1368;databasename=mydb;integratedsecurity=true;";//Windows Integrated Mode connection//Declare the JDBC objects. Connection con =NULL; Statement stmt=NULL; ResultSet RS=NULL; Try {                  //establish the connection. SYSTEM.OUT.PRINTLN ("begin.")); Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); Con=drivermanager.getconnection (URL); System.out.println ("End."); //Create and execute an SQL statement that returns some data. String SQL = "Select TOP Ten * from Aud_t_basis"; stmt=con.createstatement (); RS=stmt.executequery (SQL); //iterate through the data in the result set and display it.                  while(Rs.next ()) {System.out.println (rs.getstring (4) + "" + rs.getstring (6)); }              }                    //Handle Any errors there is a occurred.             Catch(Exception e) {e.printstacktrace (); }                    finally {                  if(rs! =NULL)                      Try{rs.close (); } Catch(Exception e) {}if(stmt! =NULL)                      Try{stmt.close (); } Catch(Exception e) {}if(Con! =NULL)                      Try{con.close (); } Catch(Exception e) {}}} }  

The second type: Mixed authentication mode, with the url2 of the Java code above.

The following actions are required in Integrated mode:

Find you just the decompression directory: into the sqljdbc_3.0\chs\auth\x64, my is 64-bit system, if it is 32-bit x86, will be a file named Sqljdbc_auth.dll Copy to: C:\Windows\System32, just fine

Finally, sqlserver2008 is using a dynamic port, which you need to configure:

Open the Configuration Tool->sqlserver Configuration Manager->sqlserver Network configuration->mssqlserver Protocol->TCP/IP enabled, remove 0 from the TCP dynamic port, leave it blank, and then pull the list to the bottom (IPALL ), configure a fixed port, you can use this port to connect to the database at a later time:



Here I use 1368, after the database restarts, you can use the above program to connect.

    • View Picture Attachments

Java Connection SqlServer2008 Database (RPM)

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.