JAVA JDBC Connection SQLServer2012

Source: Internet
Author: User

Java test code to connect to the database

1 ImportJava.sql.*;2 3  Public classConmanager {4     Final StaticString CFN = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";5     Final StaticString url = "Jdbc:sqlserver://localhost:1433;databasename=student";6     7      Public Static voidMain (string[] args) {8Connection con =NULL;9PreparedStatement statement =NULL;TenResultSet res =NULL; One         Try { A Class.forName (CFN); -con = drivermanager.getconnection (URL, "sa", "1234"); -              theString sql = "Select *from test";//Query Test Table -statement =con.preparestatement (SQL); -res =statement.executequery (); -              while(Res.next ()) { +String title = res.getstring ("Test_name");//gets the elements of the Test_name column ; -System.out.println ("Name:" +title); +             } A              at}Catch(Exception e) { -             //Todo:handle Exception - e.printstacktrace (); -}finally{ -             Try { -                 if(Res! =NULL) Res.close (); in                 if(Statement! =NULL) Statement.close (); -                 if(Con! =NULL) Con.close (); to}Catch(Exception E2) { +                 //Todo:handle Exception - e2.printstacktrace (); the             } *         } $     }Panax Notoginseng}

To import the Sqljdbc4.jar package in a Java project

Project name right mouse button->build path->configure Build Path

Add Sqljdbc4.jar

Add success

The next step is to process the SQLSERVER2012 database

1. Ensure the normal start of service

2. Ensure that these are available in the database

Running a Java project

Database connection Success!!!

JAVA JDBC Connection SQLServer2012

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.