A simple Java connection to the SQL Server database connection driver class

Source: Internet
Author: User

ImportJava.sql.*;/*** SQL Server database connection class *@authorAdministrator **/ Public classSQLSDC {Static intA = 0;  PublicConnection SQLSDC (string user, string pwd, String dn) {string URL= "Jdbc:sqlserver://localhost:1433;databasename=" +DN; FinalString driverclassname = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; @SuppressWarnings ("Unused") String UserName=user; String Password=pwd; //Connection ObjectConnection con =NULL; Try{class.forname (driverclassname); Con=drivermanager.getconnection (URL, user, password); //System.out.println ("\ n success ... \ n");}Catch(SQLException e) {System.err.println ("\ nthe error occurred and the database service is not open!" "); A=1; }Catch(ClassNotFoundException e) {System.err.println ("\ nthe connection to the database failed!" ");        E.printstacktrace (); }        returncon; }        //return to open state     Public intfail () {returnA; }        //Close Connection     Public voidClose (ResultSet RT, Statement St, Connection con)throwsSQLException {if(rt!=NULL) {rt.close (); }        if(st!=NULL) {st.close (); }        if(con!=NULL) {con.close (); }                    }    }


A simple Java connection to the SQL Server database connection driver class, where three parameters user represents the login database username, pwd indicates the login password, DN indicates the database name of the connection.

The SQLSDC method returns a Connection object Connection; The Fail method returns the database connection state; The Close method closes all connections.

A simple Java connection to the SQL Server database connection driver class

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.