在java中使用mysql

來源:互聯網
上載者:User

標籤:

在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程式中使用

建立資料庫並切換至建立的資料庫

 
  1. create database feedback;
  2. use feedback;
  3. CREATE USER sqluser IDENTIFIED BY ‘sqluserpw‘;
  4. grant usage on *.* to [email protected] identified by ‘sqluserpw‘;
  5. grant all privileges on feedback.* to [email protected];
  6. CREATE TABLE comments (id INT NOT NULL AUTO_INCREMENT,
  7. MYUSER VARCHAR(30) NOT NULL,
  8. EMAIL VARCHAR(30),
  9. WEBPAGE VARCHAR(100) NOT NULL,
  10. DATUM DATE NOT NULL,
  11. SUMMARY VARCHAR(40) NOT NULL,
  12. COMMENTS VARCHAR(400) NOT NULL,
  13. PRIMARY KEY (ID));
  14. 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就請自行解決啦.

在代碼中串連資料庫並且進行查詢
 
  1. private Connection connect = null;
  2. Class.forName("com.mysql.jdbc.Driver");
  3. // Setup the connection with the DB
  4. //3306是連接埠號碼(TCP port),feedback為使用的資料庫(上面已經建立了),password需要替換
  5. connect = DriverManager
  6. .getConnection("jdbc:mysql://localhost:3306/feedback?"
  7. + "user=root&password=your_passworld");
  8. statement = connect.createStatement();
  9. // Result set get the result of the SQL query

串連資料庫後就可以執行一些相關的sql語句了,比如使用statement進行查詢操作:

 
  1. Statement statement = connect.createStatement();
  2. ResultSet resultSet = statement
  3. .executeQuery("select * from feedback.comments");

比如使用PreparedStatement進行插入操作

 
  1. PreparedStatement preparedStatement = connect
  2. .prepareStatement("insert into feedback.comments values (default, ?, ?, ?, ? , ?, ?)");
  3. // "myuser, webpage, datum, summery, COMMENTS from feedback.comments");
  4. // Parameters start with 1
  5. preparedStatement.setString(1, "Test");
  6. preparedStatement.setString(2, "TestEmail");
  7. preparedStatement.setString(3, "TestWebpage");
  8. preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
  9. preparedStatement.setString(5, "TestSummary");
  10. preparedStatement.setString(6, "TestComment");
  11. preparedStatement.executeUpdate();

使用PreparedStatement進行查詢操作

 
  1. preparedStatement = connect
  2. .prepareStatement("SELECT myuser, webpage, datum, summary, COMMENTS from feedback.comments");
  3. resultSet = preparedStatement.executeQuery();

ResultSet是一個類似迭代器的東西,可以對查詢結果進行遍曆

 
  1. while (resultSet.next()) {
  2. // It is possible to get the columns via name
  3. // also possible to get the columns via the column number
  4. // which starts at 1
  5. // e.g. resultSet.getSTring(2);
  6. String user = resultSet.getString("myuser");
  7. String website = resultSet.getString("webpage");
  8. String summery = resultSet.getString("summary");
  9. Date date = resultSet.getDate("datum");
  10. String comment = resultSet.getString("comments");
  11. System.out.println("User: " + user);
  12. System.out.println("Website: " + website);
  13. System.out.println("Summery: " + summery);
  14. System.out.println("Date: " + date);
  15. System.out.println("Comment: " + comment);
  16. }
參考資料

http://www.vogella.com/tutorials/MySQLJava/article.html
http://www.vogella.com/tutorials/MySQL/article.html

源碼下載

CSDN



來自為知筆記(Wiz)

在java中使用mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.