Java connection mysql database java connection SQL server database, mysqlsql
In java applications, we often perform necessary operations on the database. Next we will learn how to use java to connect to the mysql database and java to connect to the SQL server database.
I. mysql
Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; public class TestOne {private static Connection connection; private static Statement statement; private static ResultSet result; public static void main (String [] args) {try {// load the jdbc Driver Class. forName ("com. mysql. jdbc. driver "); // specify the Host Name (127.0.0.1 by default) and the port number (33 by default ). 06) and database name (must be specified) String url = "jdbc: mysql: // localhost: 3306/test1"; // establish a connection with the database connection = DriverManager. getConnection (url, "root", "123456"); // create a Statement object to send the SQL statement to the database Statement = connection. createStatement (); // return the query result to result = statement.exe cuteQuery ("select * from user"); while (result. next () {System. out. println ("name:" + result. getString (1) + "password:" + result. getString (2);} con Nection. close (); result. close (); statement. close ();} catch (ClassNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {try {if (connection! = Null) connection. close (); if (result! = Null) result. close (); if (statement! = Null) statement. close ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}}/*** mysql> select * from user; * + ---------- + * | name | password | * + ---------- + * | lisi | 123456 | * | wangwu | 123456 | * | zhangsan | 123456 | * + ---------- + ---------- + * 3 rows in set (0.54 sec) ** output result in java * name: lisi password: 123456 * name: wangwu password: 123456 * name: zhangsan password: 123456 */
Ii. SQL server
Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; public class TestDemo {public static void main (String [] args) {String url = "jdbc: SQL SERVER: // localhost: 1433; DatabaseName = Contellation "; connection conn = null; try {Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver "); conn = DriverManager. getConnection (url, "sa", ""); Statement statement = conn. createStatement (); ResultSet rs = statement.exe cuteQuery ("select * from dbo. login table "); while (rs. next () {System. out. println ("username:" + rs. getString (1) + "Password:" + rs. getString (2);} conn. close ();} catch (ClassNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}/ *** output result in java * User name: John's password: 123456 * User name: Li Si password: 111111 * User name: Wang Wu password: 123654 * User Name: wang yanyao password: 0123456789 * User name: Zeng 'an new password: 123456 */
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.