在java的應(yīng)用中,我們經(jīng)常會(huì)對數(shù)據(jù)庫進(jìn)行必要的操作,下來我們就了解一下如何用java連接mysql數(shù)據(jù)庫 以及java連接sql server數(shù)據(jù)庫
一、mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
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 { //加載jdbc驅(qū)動(dòng)程序 Class.forName( "com.mysql.jdbc.Driver" ); //指明主機(jī)名(默認(rèn)為:127.0.0.1)和端口號(默認(rèn)為:3306)以及數(shù)據(jù)庫名(必須指定) String url = "jdbc:mysql://localhost:3306/test1" ; //與數(shù)據(jù)庫建立連接 connection = DriverManager.getConnection(url, "root" , "123456" ); //創(chuàng)建一個(gè)Statement對象將SQL語句發(fā)送到數(shù)據(jù)庫 statement = connection.createStatement(); //將查詢結(jié)果返回給result result = statement.executeQuery( "select *from user" ); while (result.next()){ System.out.println( "name:" + result.getString( 1 ) + " password:" + result.getString( 2 )); } connection.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) * *在java中的輸出結(jié)果 *name:lisi password:123456 *name:wangwu password:123456 *name:zhangsan password:123456 */ |
二、sql server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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:sqlserver://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.executeQuery( "select * from dbo.登陸表 " ); while (rs.next()){ System.out.println( "用戶名:" + rs.getString( 1 ) + " 密碼:" + 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(); } } } /** * java中的輸出結(jié)果 * 用戶名:張三 密碼:123456 *用戶名:李四 密碼:111111 *用戶名:王五 密碼:123654 *用戶名:王延暾 密碼:0123456789 *用戶名:曾安新 密碼:123456 */ |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。