Java Connection SQL database

Source: Internet
Author: User

Recently, we've also looked at the contents of many Java connection databases.

Summarized as follows

1 http://blog.csdn.net/stewen_001/article/details/19553173/

2 http://blog.163.com/jackie_howe/blog/static/19949134720125173539380/

3 http://blog.sina.com.cn/s/blog_866c5a5d0101as9k.html

4 http://zhidao.baidu.com/link?url=wIFKTTwtBKPOtPgahIx0K70zFxloEEdytx0KrfjfHlf6SeQVgbTbwXHBqUQ5H8iIiMvQeK1a6LHSmkR-X82C1a

5 HTTP://WWW.TUICOOL.COM/ARTICLES/N6JNJQ

6 http://www.cnblogs.com/496963524-zhangying/articles/2232599.html

7 Http://zhidao.baidu.com/link?url=MTjKeZcR_ZVJgYTjsyduHgiq3LQqKuS7hs-sIUQJNBug1Ap0nHvdJ0uremwy_BsYRMsM0CJ6fy5MNNm8vY0naa

8 http://jingyan.baidu.com/article/90895e0fb32e5f64ed6b0b49.html

9 http://weistar.iteye.com/blog/1744871

Ten http://www.cnblogs.com/zhijianliutang/p/4085546.html

The concrete steps are summarized as follows

1 Download the JDBC driver package first Sqljdbc4.jar

Unzip and unzip under the Tomcat framework under each JDK also need to copy

2 Opening the Telenet service

First click on Add Telnet Service + client on Microsoft System

Then start-"run-" Services.msc open the Telnet service

Two exceptions are usually encountered here

Exception 1 Error 1433

Workaround 1 Configure the port number configuration details for TCP/IP in your sqlexpress protocol See site 1 above

2 Open all protocols for your SQL Server

3 Windows7 Unable to start the Telnet service, error 1068 occurred

After you turn on Telnet Server and client in [Control Panel-Programs-turn Windows features on or off],
In the Run window, enter "Services.msc" to open the service window, right-telnet--start, error 1068, such as: Right-telnet--Property-Dependency, check "This service depends on the following system components" in the individual components have been started, to ensure that the After a component is in the boot state, right-click telnet--start
I have three dependencies the first item does not start cause open startup dependency First item I didn't report the error.

Exception 2 Error 13

Workaround 1 Click the Log On tab, select this account, and then type the NT authority\localservice password to empty the app to determine the Telnet service to start again

2 Method 1 is to access the user of the machine to fill in the access network is not so filled


After resolving 2 exceptions, you can access the Telnet service.

The verification method is as follows

Start Menu → run cmd → input: Telnet 127.0.0.1 1433, (note that there is a space between Telnet and 127, there is a space between 1 and 1433)

3 Configuring Environment variables

Right- Click My Computer → properties → advanced system settings (Advanced) → environment variables, Double-click the CLASSPATH variable in the system variable (or select classpath → Edit), append ";D: \sqljdbc4 \sqljdbc4.jar" in the Last Face (Note that there is one at the front; If there is no classpath, create a new CLASSPATH variable and set its value to "D:\sqljdbc4 \sqljdbc4.jar".

The address can be set to the address you put in the inside of the JDK in case you use it again later

4 Other related configurations

(1) We need to copy the Sqljdbc4.jar class library file to the D:\Program Files\java\jdk1.7.0\jre\lib\ext directory. (see which disk you installed, if it is C, then the first d is changed to C, the same as below)

(2) We need to copy the Sqljdbc4.jar class library files to the D:\Program Files\java\jre7\lib\ext directory It is best, as long as the JRE folder, copy a Sqljdbc4.jar to Jre7\lib\ext!!

(3) If you are using Tomcat as a server (I am using TOMCAT7), then we need to copy the Sqljdbc4.jar class library files to the C:\apache-tomcat-7.0.11\lib directory.

(4) If you are using Tomcat as a server, then we need to copy the Sqljdbc4.jar class library file to D:\apache-tomcat-7.0.11\webapps\gaofei\WEB-INF\ Lib directory (Gaofei directory is my app, this path is sure you will see)

Attention, only Sqljdbc4.jar !! If you put Sqljdbc.jar and Sqljdbc4.jar together, then even if you do it all right, it will persist. "This driver does not support JRE1.7, please use the Sqljdbc4.jar class library that supports JDBC4.0" issue. Because the JDK chooses Sqljdbc.jar by default (as I mentioned earlier, leaving only Sqljdbc4.jar).

5 Configuring in MyEclipse

Add jar packages in the project when used Sqljdbc4.jar

Then you can use a small Java engineering test

Import java.sql.*;p Ublic class DataConnection {public static void main (string[] args) {String driver= "COM.M      Icrosoft.sqlserver.jdbc.SQLServerDriver ";     String dburl= "jdbc:sqlserver://localhost:1433;databasename=mbook;user=sa;password=123"; try {class.forname (driver);//Load driver} catch (ClassNotFoundException e) {System.out.println ("load     Database driver failed ");     } System.out.println ("Load Database driver succeeded"); try {//connection conn=drivermanager.getconnection (dburl);//Connect Database Connection Conn=drivermanager.getconnecti      On ("Jdbc:sqlserver://localhost:1433;databasename=mbook", "sa", "123"); SYSTEM.OUT.PRINTLN ("Database connection succeeded!      ");      Statement st=conn.createstatement ();      System.out.println ("Start reading Database");      ResultSet rs=st.executequery ("SELECT * from Login");      while (Rs.next ()) {System.out.println (rs.getstring ("name") + "" +rs.getstring ("password"));//Read Only the UserID and Name column data in the table      } System.out.println ("read Complete");      Rs.close (); St.cloSE ();     Conn.close ();     } catch (SQLException e) {e.printstacktrace (); }    }  }

It is important to note that the name of the 1 database changes according to itself

Tens of 2 of Dbc:sqlserver://localhost:1433;databasename=mbook in Microsoft

3 SA "123" SQL User's additions vary according to their actual situation

If unable to access please follow the http://www.cnblogs.com/496963524-zhangying/articles/2232599.html configuration to overcome the exception


Java Connection SQL database

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.