資料庫尋找操作-java

來源:互聯網
上載者:User

標籤:store   建立資料庫   etc   資料庫連接   nec   語句   ack   exce   nts   

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class SqlDemo {public static void main(String[] args) {try {//1、載入驅動程式  //驅動程式名稱: com.mysql.jdbc.DriverClass.forName("com.mysql.jdbc.Driver");//2、建立和資料庫的串連//sql使用者名稱稱:root //sql使用者密碼:8273322Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/store?characterEncoding=utf8&useSSL=false", "root", "8273322");//(1)尋找操作:String sql = "select * from good_list";/* Statement 是 Java 執行資料庫操作的一個重要介面,用於在已經建立資料庫連接的基礎上,向資料庫發送要執行的SQL語句。Statement對象,用於執行不帶參數的簡單SQL語句*/Statement stmt = con.createStatement();/*ResultSet,資料庫結果集的資料表,通常通過執行查詢資料庫的語句產生。*/ResultSet rs = stmt.executeQuery(sql);//遍曆輸出while (rs.next()) {//或者:int id = rs.getInt("id"); id為資料區段名int id = rs.getInt(1);String name = rs.getString(2);String pw = rs.getString(3);System.out.println(id+" "+name+" "+pw);}//關閉rs.close();stmt.close();con.close();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}}

資料庫尋找操作-java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.