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 JSP JDBC connects to SQL Server 2005

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how JSP JDBC connects to SQL Server 2005. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Download and install

1 、 SQL Server 2005 Express Edition

Download: http://msdn.microsoft.com/vstudio/express/sql/download/

Set the ICP/IP protocol to start after installing the database, as follows:

(1) Open SQL Server Configuration Manager

(2) go to SQL Server 2005 Network Configuration- > Protocols for SQLEXPRESS

(3) set TCP/IP to Enabled (enabled)

(4) double-click the TCP/IP entry to go to the IP Addresses page

(5) set TCP Port to 1433 in IP All

(6) restart the service

2. SQL Server2005 database JSP JDBC driver

Download: http://download.microsoft.com/download/1/c/a/1cae7cc0-c010-4e0c-b1b8-7915360ee0b9/sqljdbc_1.0.809.102_chs.exe

Install or decompress to get the sqljdbc.jar file, which is the JDBC driver. Put sqljdbc.jar on classpath. (put under WEB-INF/lib in web application)

Second, the Java code to connect to the database SQL Server2005

1. Create a test data table in tempdb

Use tempdb CREATE TABLE dbo.Table_1 (ID bigint NOT NULL, NAME varchar (20) NOT NULL, EMAIL varchar (50) NULL) ON [PRIMARY]

2. Test using database connection

The following code creates a connection to the database and uses the connection to manipulate the database.

Package cn.afss.common.web.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; public class TestConnSql2k5Bean {private static final Logger logger = Logger.getLogger (TestConnSql2k5Bean.class); private Connection conn = null; public TestConnSql2k5Bean () {super () } public void getConnection () {try {Class .forName ("com.microsoft.SQL Server.jdbc.SQL ServerDriver") .newInstance (); String URL = "jdbc:SQL Server://localhost:1433;DatabaseName=tempdb"; String USER = "sa"; / / set up according to the database connection user you set up String PASSWORD = "123456" / / set conn = DriverManager.getConnection (URL, USER, PASSWORD) according to your own database connection password;} catch (java.lang.ClassNotFoundException ce) {logger.error ("Get Connection error:", ce);} catch (java.sql.SQLException se) {logger.error ("Get Connection error:", se);} catch (Exception e) {logger.error ("Get Connection error:", e) } public void testConnection () {if (conn = = null) this.getConnection (); try {String sql = "SELECT * FROM TABLE_1"; Statement stmt = conn.createStatement (); ResultSet rs = stmt.executeQuery (sql); while (rs.next ()) {logger.debug (rs.getString ("ID")); logger.debug (rs.getString ("NAME")) Logger.debug (rs.getString ("EMAIL"));} rs.close (); stmt.close ();} catch (SQLException e) {logger.error (e.getMessage (), e);} finally {if (conn! = null) try {conn.close () } catch (SQLException e) {}} public static void main (String [] args) {TestConnSql2k5Bean bean = new TestConnSql2k5Bean (); bean.testConnection ();}}

Third, the difference of connection code between SQL Server2000 and 2005

Note the difference between 2000 and 2005 when writing connection code:

1. JSP JDBC connects to SQL Server2000

Class.forName ("com.microsoft.jdbc.SQL Server.SQL ServerDriver") .newInstance () URL = "jdbc:microsoft:SQL Server://localhost:1433;DatabaseName=tempdb"

2. JSP JDBC connects to SQL Server2005

Class.forName ("com.microsoft.SQL Server.jdbc.SQL ServerDriver"). NewInstance (); URL = "jdbc:SQL Server://localhost:1433;DatabaseName=tempdb"; Thank you for reading! This is the end of this article on "how to connect JSP JDBC to SQL Server 2005". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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