2016年4月22日 星期五
2016年4月20日 星期三
4
●Eclipse ---> Class 如圖
●設定class name跟package name
●載入JDBC-Drive
●程式碼
try {
Class.forName("com.mysql.jdbc.Driver");
//註冊driver
con = DriverManager.getConnection(
"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5",
"root","12345");
//取得connection
//jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5
//localhost是主機名,test是database名
//useUnicode=true&characterEncoding=Big5使用的編碼
}
catch(ClassNotFoundException e)
{
System.out.println("DriverClassNotFound :"+e.toString());
}//有可能會產生sqlexception
catch(SQLException x) {
System.out.println("Exception :"+x.toString());
}
|
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcmysql {
private Connection con = null; //Database objects
//連接object
private Statement stat = null;
//執行,傳入之sql為完整字串
private ResultSet rs = null;
//結果集
private PreparedStatement pst = null;
//執行,傳入之sql為預儲之字申,需要傳入變數之位置
//先利用?來做標示
private String dropdbSQL = "DROP TABLE User ";
private String createdbSQL = "CREATE TABLE User (" +
" id INTEGER " +
" , name VARCHAR(20) " +
" , passwd VARCHAR(20))";
private String insertdbSQL = "insert into User(id,name,passwd) " +
"select ifNULL(max(id),0)+1,?,? FROM User";
private String selectSQL = "select * from User ";
public jdbcmysql()
{
try {
Class.forName("com.mysql.jdbc.Driver");
//註冊driver
con = DriverManager.getConnection(
"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5",
"root","12345");
//取得connection
//jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5
//localhost是主機名,test是database名
//useUnicode=true&characterEncoding=Big5使用的編碼
}
catch(ClassNotFoundException e)
{
System.out.println("DriverClassNotFound :"+e.toString());
}//有可能會產生sqlexception
catch(SQLException x) {
System.out.println("Exception :"+x.toString());
}
}
//建立table的方式
//可以看看Statement的使用方式
public void createTable()
{
try
{
stat = con.createStatement();
stat.executeUpdate(createdbSQL);
}
catch(SQLException e)
{
System.out.println("CreateDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//新增資料
//可以看看PrepareStatement的使用方式
public void insertTable( String name,String passwd)
{
try
{
pst = con.prepareStatement(insertdbSQL);
pst.setString(1, name);
pst.setString(2, passwd);
pst.executeUpdate();
}
catch(SQLException e)
{
System.out.println("InsertDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//刪除Table,
//跟建立table很像
public void dropTable()
{
try
{
stat = con.createStatement();
stat.executeUpdate(dropdbSQL);
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//查詢資料
//可以看看回傳結果集及取得資料方式
public void SelectTable()
{
try
{
stat = con.createStatement();
rs = stat.executeQuery(selectSQL);
System.out.println("ID\t\tName\t\tPASSWORD");
while(rs.next())
{
System.out.println(rs.getInt("id")+"\t\t"+
rs.getString("name")+"\t\t"+rs.getString("passwd"));
}
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
//完整使用完資料庫後,記得要關閉所有Object
//否則在等待Timeout時,可能會有Connection poor的狀況
private void Close()
{
try
{
if(rs!=null)
{
rs.close();
rs = null;
}
if(stat!=null)
{
stat.close();
stat = null;
}
if(pst!=null)
{
pst.close();
pst = null;
}
}
catch(SQLException e)
{
System.out.println("Close Exception :" + e.toString());
}
}
public static void main(String[] args)
{
//測看看是否正常
jdbcmysql test = new jdbcmysql();
test.dropTable();
test.createTable();
test.insertTable("yku", "12356");
test.insertTable("yku2", "7890");
test.SelectTable();
}
}
|
訂閱:
文章 (Atom)