標籤:
在java中使用mysql概略
安裝環境:windows 10, eclipse
安裝mysql
運行mysql並且建立資料庫feedback
將所需的jar添加到工程裡
在代碼中串連資料庫並且進行查詢
安裝mysql:mysql-install-community下載下載後
安裝sqlserver就行了,其他的應該暫時還不需要用到,最後需要設定密碼,要記住,在後面我們需要使用帳號密碼登陸資料庫.
運行mysql,建立資料庫登陸資料庫
進入mysql的安裝目錄下,進入bin目錄,預設應該為C:\Program Files\MySQL\MySQL Server 5.6\bin,輸入
mysql -u root -p
輸入你的密碼
此時應該顯示一大串資料庫相關的內容,這個時候我們就可以輸入sql語句了.
接下來我們建立資料庫以便等下在java程式中使用
建立資料庫並切換至建立的資料庫
create database feedback;
use feedback;
CREATE USER sqluser IDENTIFIED BY ‘sqluserpw‘;
grant usage on *.* to [email protected] identified by ‘sqluserpw‘;
grant all privileges on feedback.* to [email protected];
CREATE TABLE comments (id INT NOT NULL AUTO_INCREMENT,
MYUSER VARCHAR(30) NOT NULL,
EMAIL VARCHAR(30),
WEBPAGE VARCHAR(100) NOT NULL,
DATUM DATE NOT NULL,
SUMMARY VARCHAR(40) NOT NULL,
COMMENTS VARCHAR(400) NOT NULL,
PRIMARY KEY (ID));
INSERT INTO comments values (default, ‘lars‘, ‘[email protected]‘,‘http://www.vogella.com‘, ‘2009-09-14 10:33:11‘, ‘Summary‘,‘My first comment‘);
這樣,我們就建立了一個名稱為feedba的資料庫,並且建立了一個表table,其行分別為id,myuser,email,webpage,datum,summary,comments,primary.
我們還需要最後一步操作,輸入
status
找到TCPport這一行,記住連接埠號碼.
將所需要的jar包添加到項目中
需要的jar包下載:mysql-connector-java下載
怎麼匯入到eclipse就請自行解決啦.
在代碼中串連資料庫並且進行查詢
private Connection connect = null;
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
//3306是連接埠號碼(TCP port),feedback為使用的資料庫(上面已經建立了),password需要替換
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/feedback?"
+ "user=root&password=your_passworld");
statement = connect.createStatement();
// Result set get the result of the SQL query
串連資料庫後就可以執行一些相關的sql語句了,比如使用statement進行查詢操作:
Statement statement = connect.createStatement();
ResultSet resultSet = statement
.executeQuery("select * from feedback.comments");
比如使用PreparedStatement進行插入操作
PreparedStatement preparedStatement = connect
.prepareStatement("insert into feedback.comments values (default, ?, ?, ?, ? , ?, ?)");
// "myuser, webpage, datum, summery, COMMENTS from feedback.comments");
// Parameters start with 1
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "TestEmail");
preparedStatement.setString(3, "TestWebpage");
preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
preparedStatement.setString(5, "TestSummary");
preparedStatement.setString(6, "TestComment");
preparedStatement.executeUpdate();
使用PreparedStatement進行查詢操作
preparedStatement = connect
.prepareStatement("SELECT myuser, webpage, datum, summary, COMMENTS from feedback.comments");
resultSet = preparedStatement.executeQuery();
ResultSet是一個類似迭代器的東西,可以對查詢結果進行遍曆
while (resultSet.next()) {
// It is possible to get the columns via name
// also possible to get the columns via the column number
// which starts at 1
// e.g. resultSet.getSTring(2);
String user = resultSet.getString("myuser");
String website = resultSet.getString("webpage");
String summery = resultSet.getString("summary");
Date date = resultSet.getDate("datum");
String comment = resultSet.getString("comments");
System.out.println("User: " + user);
System.out.println("Website: " + website);
System.out.println("Summery: " + summery);
System.out.println("Date: " + date);
System.out.println("Comment: " + comment);
}
參考資料
http://www.vogella.com/tutorials/MySQLJava/article.html
http://www.vogella.com/tutorials/MySQL/article.html
源碼下載
CSDN
來自為知筆記(Wiz)
在java中使用mysql