Java jdbc訪問sqlserver,oracle數據庫,jdbcsqlserver
1.JDBC訪問Oracle數據庫

![]()
1 public class Jdbc_Oracle {
2
3 // 靜態代碼塊,只會執行一次,類似C#靜態構造方法
4 static {
5 try {
6 // 加載數據庫驅動一次
7 Class.forName("oracle.jdbc.driver.OracleDriver");
8 } catch (ClassNotFoundException e) {
9 e.printStackTrace();
10 }
11 }
12
13 //main函數,數據的操作
14 public static void main(String[] args) {
15 del();
16 //exec();
17 select();
18 }
19
20 // 添加、增刪改
21 public static void exec() {
22 Connection con = null;
23 PreparedStatement cmd = null;
24 try {
25 // 在控制台輸入
26 Scanner scanner = new Scanner(System.in);
27 System.out.print("請輸入類型名稱:");
28 String name = scanner.nextLine();
29
30 // 建立數據庫連接,指定數據庫用戶名,密碼,數據庫名稱
31 con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
32 // 創建sql命令對象
33 cmd = con.prepareStatement("insert into ProductType(Name,Up) values(?,?)");
34 // 設置參數
35 cmd.setString(1, name);
36 cmd.setInt(2, 0);
37 // 執行sql返回影響行數
38 int result = cmd.executeUpdate();
39 System.out.println("影響行數:" + result);
40 } catch (Exception e) {
41 // 把錯誤的堆棧信息顯示在控制台
42 e.printStackTrace();
43 } finally {
44 try {
45 cmd.close();
46 con.close();
47 } catch (Exception e) {
48 e.printStackTrace();
49 }
50 }
51 }
52
53 // 刪除、增刪改
54 public static void del() {
55 Connection con = null;
56 PreparedStatement cmd = null;
57 try {
58 // 建立數據庫連接,指定數據庫用戶名,密碼,數據庫名稱
59 con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
60 // 創建sql命令對象
61 cmd = con.prepareStatement("delete from ProductType where Id=?");
62 // 設置參數
63 cmd.setInt(1, 21);
64 // 執行sql返回影響行數
65 int result = cmd.executeUpdate();
66 System.out.println("影響行數:" + result);
67 } catch (Exception e) {
68 // 把錯誤的堆棧信息顯示在控制台
69 e.printStackTrace();
70 } finally {
71 try {
72 cmd.close();
73 con.close();
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77 }
78 }
79
80 // 查詢
81 public static void select() {
82 Connection con = null;
83 PreparedStatement cmd = null;
84 ResultSet result = null;
85 try {
86 // 建立數據庫連接,指定數據庫用戶名,密碼,數據庫名稱
87 con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
88 // 創建sql命令對象
89 cmd = con.prepareStatement(
90 "select id, name, up from producttype where Id>? and Name like ?");
91 // 設置參數
92 cmd.setInt(1, 5);
93 cmd.setString(2, "%能%");
94 // 執行sql獲得結果集
95 result = cmd.executeQuery();
96 // 取出結果集中的數據
97 while (result.next()) {
98 System.out.print(result.getInt("Id") + "\t");
99 System.out.print(result.getInt(1) + "\t");
100 System.out.print(result.getString("Name") + "\t");
101 System.out.print(result.getInt("Up") + "\t\n");
102 }
103 } catch (Exception e) {
104 e.printStackTrace();
105 } finally {
106 try {
107 result.close();
108 cmd.close();
109 con.close();
110 } catch (Exception e) {
111 e.printStackTrace();
112 }
113 }
114 }
115 }
116
117 // 刪除、增刪改
118 public static void del() {
119 Connection con = null;
120 PreparedStatement cmd = null;
121 try {
122 // 建立數據庫連接,指定數據庫用戶名,密碼,數據庫名稱
123 con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "gmall", "orcl");
124 // 創建sql命令對象
125 cmd = con.prepareStatement("delete from ProductType where Id=?");
126 // 設置參數
127 cmd.setInt(1, 21);
128 // 執行sql返回影響行數
129 int result = cmd.executeUpdate();
130 System.out.println("影響行數:" + result);
131 } catch (Exception e) {
132 // 把錯誤的堆棧信息顯示在控制台
133 e.printStackTrace();
134 } finally {
135 try {
136 cmd.close();
137 con.close();
138 } catch (Exception e) {
139 e.printStackTrace();
140 }
141 }
142 }
143
144 // 查詢
145 public static void select() {
146 Connection con = null;
147 PreparedStatement cmd = null;
148 ResultSet result = null;
149 try {
150 // 建立數據庫連接,指定數據庫用戶名,密碼,數據庫名稱
151 con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "gmall", "orcl");
152 // 創建sql命令對象
153 cmd = con.prepareStatement(
154 "select id, name, up from producttype where Id>? and Name like ?");
155 // 設置參數
156 cmd.setInt(1, 5);
157 cmd.setString(2, "%能%");
158 // 執行sql獲得結果集
159 result = cmd.executeQuery();
160 // 取出結果集中的數據
161 while (result.next()) {
162 System.out.print(result.getInt("Id") + "\t");
163 System.out.print(result.getInt(1) + "\t");
164 System.out.print(result.getString("Name") + "\t");
165 System.out.print(result.getInt("Up") + "\t\n");
166 }
167 } catch (Exception e) {
168 e.printStackTrace();
169 } finally {
170 try {
171 result.close();
172 cmd.close();
173 con.close();
174 } catch (Exception e) {
175 e.printStackTrace();
176 }
177 }
178 }
179 }
View Code
2.JDBC訪問sqlserver數據庫

![]()
1 public class Jdbc_SQLServer {
2
3 // 靜態代碼塊,只會執行一次,類似C#靜態構造方法
4 static {
5 try {
6 // 加載驅動一次
7 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
8 } catch (ClassNotFoundException e) {
9 e.printStackTrace();
10 }
11 }
12
13 public static void main(String[] args) {
14 del();
15 //exec();
16 select();
17 }
18
19 // 添加、增刪改
20 public static void exec() {
21 Connection con = null;
22 PreparedStatement cmd = null;
23 try {
24 // 在控制台輸入
25 Scanner scanner = new Scanner(System.in);
26 System.out.print("請輸入類型名稱:");
27 String name = scanner.nextLine();
28
29 // 建立數據庫連接,指定數據庫用戶名,密碼,數據庫名稱
30 con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
31 // 創建sql命令對象
32 cmd = con.prepareStatement("insert into [ProductType]([Name],Up) values(?,?)");
33 // 設置參數
34 cmd.setString(1, name);
35 cmd.setInt(2, 0);
36 // 執行sql返回影響行數
37 int result = cmd.executeUpdate();
38 System.out.println("影響行數:" + result);
39 } catch (Exception e) {
40 // 把錯誤的堆棧信息顯示在控制台
41 e.printStackTrace();
42 } finally {
43 try {
44 cmd.close();
45 con.close();
46 } catch (Exception e) {
47 e.printStackTrace();
48 }
49 }
50 }
51
52 // 刪除、增刪改
53 public static void del() {
54 Connection con = null;
55 PreparedStatement cmd = null;
56 try {
57 // 建立數據庫連接,指定數據庫用戶名,密碼,數據庫名稱
58 con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
59 // 創建sql命令對象
60 cmd = con.prepareStatement("delete from [ProductType] where Id=?");
61 // 設置參數
62 cmd.setInt(1, 12);
63 // 執行sql返回影響行數
64 int result = cmd.executeUpdate();
65 System.out.println("影響行數:" + result);
66 } catch (Exception e) {
67 // 把錯誤的堆棧信息顯示在控制台
68 e.printStackTrace();
69 } finally {
70 try {
71 cmd.close();
72 con.close();
73 } catch (Exception e) {
74 e.printStackTrace();
75 }
76 }
77 }
78
79 // 查詢
80 public static void select() {
81 Connection con = null;
82 PreparedStatement cmd = null;
83 ResultSet result = null;
84 try {
85 // 建立數據庫連接,指定數據庫用戶名,密碼,數據庫名稱
86 con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
87 // 創建sql命令對象
88 cmd = con.prepareStatement(
89 "SELECT [Id],[Name],[Up] FROM [GoMall].[dbo].[ProductType] where Id>? and Name like ?");
90 // 設置參數
91 cmd.setInt(1, 5);
92 cmd.setString(2, "%能%");
93 // 執行sql獲得結果集
94 result = cmd.executeQuery();
95 // 取出結果集中的數據
96 while (result.next()) {
97 System.out.print(result.getInt("Id") + "\t");
98 System.out.print(result.getInt(1) + "\t");
99 System.out.print(result.getString("Name") + "\t");
100 System.out.print(result.getInt("Up") + "\t\n");
101 }
102 } catch (Exception e) {
103 e.printStackTrace();
104 } finally {
105 try {
106 result.close();
107 cmd.close();
108 con.close();
109 } catch (Exception e) {
110 e.printStackTrace();
111 }
112 }
113 }
114
115 }
View Code