Databases are often used during development. If we separate the functional code for connecting to the database that we have written, we can easily reference it everywhere without writing duplicate code.
Note that the MSSQL2000 connection driver is different from the 2000 and 2005 drivers above 2008. The Jar packages to be imported are also different.
The following is the code.
Package usage; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException;/***** @ Date: 04:50:40, January 14, * @ Author: gyh * @ TODO: Functional SQL server for connecting java to databases, mysql) */public class DBConnection {// SQL server 2000 driver address public static final String SQL _SERVER_2000_DRIVER = "com. microsoft. jdbc. sqlserver. SQLServerDriver "; // SQL server 2005 driver address public static final String SQL _SERVER_20 05_DRIVER = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; // SQL server 2008 driver address public static final String SQL _SERVER_2008_DRIVER =" com. microsoft. sqlserver. jdbc. SQLServerDriver "; // mysql driver address public static final String MYSQL_DRIVER =" com. mysql. jdbc. driver ";/*** database address: localhost can be used to replace the actual IP address of your database with a local IP address, which can be used for learning or 127.0.0.1). * test is your database name, depending on the actual situation, port 1433 is the database port, which can be viewed in SQL server configure center. */Public static final String SQL _SERVER_URL = "jdbc: sqlserver: // localhost: 1433; databasename ="; public static final String MYSQL_URL = "jdbc: mysql: // localhost: 3306/"; private Connection conn;/*** load driver ** @ param DriverName * driver name */public DBConnection (String DriverName) throws ClassNotFoundException {Class. forName (DriverName);}/*** by default, log on to the SQL server with no logon password ** @ param databaseName * database name * @ return * @ throw S SQLException */public Connection connectToMSSQL (String databaseName) throws SQLException {conn = DriverManager. getConnection (SQL _SERVER_URL + databaseName, "sa", ""); return conn ;} /*** connect to the SQL server ** @ param dbName * database name * @ param username * database login name * @ param password * database login password * @ return * @ throws SQLException */ public Connection connectToMSSQL (String dbName, string username, String password) throws SQLEx Ception {conn = DriverManager. getConnection (SQL _SERVER_URL + dbName, username, password); return conn;}/*** connect to the mysql server, default port 3306 ** @ param username * @ param password * logon password * @ return * @ throws SQLException */public Connection connectToMysql (String dbName, String username, String password) throws SQLException {conn = DriverManager. getConnection (MYSQL_URL + dbName, username, password); return conn;} public Void close () throws SQLException {if (conn! = Null) conn. close ();} public static void main (String arsg []) {try {new DBConnection (DBConnection. SQL _SERVER_2000_DRIVER ). connectToMSSQL ("test");} catch (SQLException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace ();}}}
Call the getConnection class when using it.
This article from "Sir fire?" blog, please be sure to keep this source http://88qlp88.blog.51cto.com/4346152/1324947