How to use JDBC to connect Sql2008 databases in Java (GO)

Source: Internet
Author: User

In Java EE development, we must use the database, then in the development of Java EE, how to use code implementation and SQL2008 connection? In this article, I'll walk you through the simplest way to connect a SQL2008 database using JDBC.

First, let's look at some information about our database.

User name: SA

Password: 123456

Database name: Servletuser

Then let's take a quick look at the project directory

Note: The connection database needs to be imported into the external package, because my JDK is 1.7, so here I am importing Sqljdbc4.jar, this external package can be downloaded on Microsoft's website

JDBC External package: http://www.microsoft.com/zh-cn/download/details.aspx?id=21599

After the download is complete, unzip the compressed file, and then we can use the package inside.

Put Sqljdbc4.jar (my JDK is 1.7, so import this, other JDK depends on the situation) into the project.

At this point, our preparations are done, and now the code is written.

[Java]View Plaincopyprint?
  1. Package Demo;
  2. Import java.sql.Connection;
  3. Import Java.sql.DriverManager;
  4. Import java.sql.SQLException;
  5. Public class Jdbctext {
  6. Public static void Main (string[] args) {
  7. Connection dbconn = null;
  8. String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; //load JDBC driver
  9. Connecting servers and Databases Servletuser
  10. String Dburl = "jdbc:sqlserver://localhost:1433; Databasename=servletuser ";
  11. String userName = "sa"; //Default user name
  12. String userpwd = "123456"; //Password
  13. try {
  14. Class.forName (drivername);
  15. Dbconn = Drivermanager.getconnection (Dburl, UserName, userpwd);
  16. System.out.println ("Connection successful!"); //If the connection is successful
  17. } catch (Exception e) {
  18. E.printstacktrace ();
  19. }finally{
  20. To release the resources
  21. if (dbconn!=null) {
  22. try {
  23. Dbconn.close ();
  24. } catch (SQLException e) {
  25. E.printstacktrace ();
  26. }
  27. }
  28. }
  29. }
  30. }
Package Demo;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.sqlexception;public class Jdbctext {public static void main (string[] args) {Connection dbconn = null; String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; Load JDBC driver//connection server and database servletuserstring dburl = "jdbc:sqlserver://localhost:1433; Databasename=servletuser "; String userName = "sa"; Default User name string userpwd = "123456"; Password try {class.forname (drivername);d bconn = Drivermanager.getconnection (Dburl, UserName, userpwd); System.out.println ("Connection successful!"); If the connection succeeds} catch (Exception e) {e.printstacktrace ();} finally{//the release of the resource if (dbconn!=null) {try {dbconn.close ();} catch (SQLException e) {e.printstacktrace ();}}}}


The code is relatively simple, we should be able to see the easy to understand, but I did in the first process, there was a mistake

TCP/IP connection to host localhost via port 1433 failed.

This problem occurs because our SQL database meter has a TCP/IP connection enabled and we need to open the TCO/IP connection in SQL Sevice configeration Manager

After the connection is opened, we need to restart the SQL service for the configuration to work

Then start the application again and it should be successful.

Http://www.cnblogs.com/oversea201405/p/3749549.html

How to use JDBC to connect Sql2008 databases in Java (GO)

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.