1. This section describes how to connect java code to common databases, including MySQL, Sybase, Oracle, and SqlServer. To connect to a database, you must add a jar package to the project to load the driver, I have sorted these jar packages into csdn resources: download. csdn. netdetailwangduanqing594570040932,
1. This section describes how to connect java code to common databases, including MySQL, Sybase, Oracle, and SqlServer. To connect to a database, you must add a jar package to the project to load the driver, these jar packages I organized in the csdn resources,: http://download.csdn.net/detail/wangduanqing5945/7004093 2,
1. This section describes how to connect java code to common databases, including MySQL, Sybase, Oracle, and SqlServer. To connect to a database, you must add a jar package to the project to load the driver, I have sorted these jar packages into csdn resources: http://download.csdn.net/detail/wangduanqing5945/700409321_mysqlconnection
Connection connection = null;try {Class.forName("com.mysql.jdbc.Driver");connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/boxmanager", "username", "password");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}
Here, I will explain to some friends who have never used it. com. mysql. jdbc. Driver is the package name and Class Name of the Driver class in MySQL. The following getConnection method has three parameters: the first database address corresponds to a database, the second parameter is the database user name, and the third parameter is the database password. 3. Sybase connection
Try {String driver = "com. sybase. jdbc3.jdbc. sybDriver "; Class. forName (driver ). newInstance (); String url = "jdbc: sybase: Tds: 192.168.100.252: 4500/myDB"; // myDB your database name Properties sysProps = System. getProperties (); sysProps. put ("user", username); // sets the database access username sysProps. put ("password", password); // password connection = DriverManager. getConnection (url, sysProps);} catch (Exception e) {e. printStackTrace ();}
I must have understood the above explanation. I don't need to explain it here. The ideas are consistent. 4. Oracle connection
// Obtain the Connection ct = null; try {// load the driver Class. forName ("oracle. jdbc. driver. oracleDriver "); ct = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: simlink", "your username", "your pwd");} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace ();}
5. SQL server connection
String driverName = "com. microsoft. jdbc. sqlserver. SQLServerDriver "; String dbURL =" jdbc: microsoft: sqlserver: // localhost: 1433; databasename = li "; // 1433 indicates the database port, "li" is your database name String userName = "sa"; // sa is the super user of the database. It is best not to change the name. Permission problem String userPwd = "123456 "; // sa password Connection dbConn = null; try {Class. forName (driverName ). newInstance (); dbConn = DriverManager. getConnection (dbURL, userName, userPwd); System. out . Println ("connection successful! ");} Catch (Exception e) {e. printStackTrace ();}