Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JDBC to access SQL Server 2005

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use JDBC to access SQL Server 2005. I hope you will get something after reading this article. Let's discuss it together.

1. To download a JDBC driver for SQL Server. Just go to Microsoft to download, download an exe file, run that exe to extract the file to any folder, and take the sqljdbc4.jar and sqljdbc.jar in it for backup.

two。 Configure SQL Server 2005. In SQL Server 2005, the TCP/IP protocol is not enabled by default. The main purpose of this step is to enable the protocol and enable SQL Server to accept remote access. The specific steps are as follows:

(1) Open the SQLServer configuration Manager (SQLServer Configuration Manager) and in the configuration Manager go back to the SQLEXPRESS protocol (Protocols for SQLEXPRESS) under the SQLServer 2005 network configuration (SQLServer 2005 Network Configuration).

(2) double-click the TCP/IP tab to open the TCP/IP property. Set the enabled (Enable) item to Yes. Switch to the IP address tab, set the TCP Port (TCP Port) in IP ALL to 1433, and then determine.

(3) Open the SQLServer Peripheral Application Configurator (SQLServer Surface Area Configuration), then open the Peripheral Application Configurator for Services and connections, go to SQLEXPRESS- > Database Engine- > remote connection, and select "use TCP/IP only" or "use TCP/IP and named pipes at the same time" under "Local Link and remote connection". Here I chose the former, then apply and make sure.

(4) restart SQLServer service.

3. Test the connection. Students who will, please skip ~ ~

(1) set up database testDB (name is random, data table is random) in SQLServer Management Studio Express, and create table in testDB.

CREATE TABLE user (ID bigint NOT NULL, Name varchar (20) NOT NULL, Email varchar (50) NULL)

Then casually add a few pieces of data to the table.

(2) build a java application project (I use eclipse, other IDE is the same), import the SQLServer driver into the project, note, if you use JDK6, then import sqljdbc4.jar, if the lower version of JDK is imported into sqljdbc.jar, do not make a mistake, otherwise an error will be reported when connecting.

(3) use JDBC to access the test program code of SQLServer.

? [Copy to clipboard] View Code JAVA

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566package jdbc; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement; public class Test {private Connection conn = null; public Test () {super ();} public void getConnection () {try {Class.forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver") .newInstance (); String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=testDB"; String USER = "sa" / / set String PASSWORD according to your own database connection user; / / set conn = DriverManager.getConnection (URL, USER, PASSWORD) according to your own database connection password;} catch (java.lang.ClassNotFoundException ce) {System.out.println ("Get Connection error:"); ce.printStackTrace ();} catch (java.sql.SQLException se) {System.out.println ("Get Connection error:") Se.printStackTrace ();} catch (Exception e) {System.out.println ("Get Connection error:"); e.printStackTrace ();} public void testConnection () {if (conn = = null) this.getConnection (); try {String sql = "SELECT * FROM user"; Statement stmt = conn.createStatement (); ResultSet rs = stmt.executeQuery (sql); while (rs.next ()) {System.out.print (rs.getString ("ID") + "") System.out.print (rs.getString ("Name") + "); System.out.println (rs.getString (" Email "));} rs.close (); stmt.close ();} catch (SQLException e) {System.out.println (e.getMessage ()); e.printStackTrace ();} finally {if (conn! = null) try {conn.close () } catch (SQLException e) {}} public static void main (String [] args) {Test bean = new Test (); bean.testConnection ();}}

(4) run the program, if there is no accident, it should be OK. The connection code is different from that of SQLServer2000. These two sentences can be written down and set aside.

Class.forName (com.microsoft.sqlserver.jdbc.SQLServerDriver) .newInstance ()

String URL = "jdbc:sqlserver://localhost:1433;DatabaseName= database name"

JDBC access SQL Server 2005 has been set up.

After reading this article, I believe you have some understanding of "how to use JDBC to access SQL Server 2005". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report