Java Connect and manipulate SQL Server database

Source: Internet
Author: User
Tags microsoft sql server stmt

I just need to introduce the Sqljdbc4.jar package into the project.

-----------------------------------------

How to connect a SQL Server database in Java-hanghangde blog-Blog Channel-csdn.net
http://blog.csdn.net/hanghangde/article/details/50463658

This article is very good, there are links 2000 and 2008 of the server version;

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 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) {}}} }   
 PackagePkg;ImportJava.sql.*; Public classQuerydemo { Public Static voidMain (string[] args) {String jdriver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; String URL= "Jdbc:sqlserver://127.0.0.1:1433;databasename=gpsdb"; String User= "SA"; String Password= "123456"; Try{class.forname (jdriver);//dynamic import of database driversConnection conn=drivermanager.getconnection (URL, user, password);//Get Database linkString query= "Select TOP * FROM dbo. Dsc_user ";//Creating SQL statementsStatement stmt=conn.createstatement ();//Execute SQL statementResultSet rs=stmt.executequery (query);  while(Rs.next ()) {System.out.println (rs.getstring ("ID") + "\tname:" +rs.getstring (2) + "\t\t[groupid": "+rs.getstring (3)                + "\t\t[password]:" +rs.getstring (4)); } System.out.println ("Query Data Success");            Rs.close ();            Stmt.close ();        Conn.close (); }Catch(Exception e) {e.printstacktrace (); }    }}
I practice the code

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.

======================== Expansion:

Java Connection Database (mysql,sqlserver)-ji Yuan-blog
Http://www.cnblogs.com/huangxinyuan650/p/5991441.html

Java Development Related Materials-Keenweiwei's column-Blog channel-csdn.net
http://blog.csdn.net/keenweiwei/article/details/7328611

Java Connect and manipulate SQL Server database-zhangxiaoshuang-Blog Park
Http://www.cnblogs.com/zhangxiaoshuang/p/6793561.html

Mysql-connector-java-5.0.8-bin.jar
Sqljdbc.jar
Sqljdbc4.jar

Java Connect and manipulate SQL Server database

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.