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