Mysql的JDBC

來源:互聯網
上載者:User

標籤:

  Java程式可以通過JDBC連結資料庫,通過JDBC可以方便的訪問資料庫,不必為特定的資料庫編寫專門的程式。

  需要先配置mysql-connector-java-5.0.8-bin.jar

  使用JDBC串連資料庫的開發流程如下:

  1. 註冊資料庫驅動  Class.forName("com.mysql.jdbc.Driver");
  2. 構建資料庫連接的URL,Mysql的串連URL為"jdbc:mysql://localhost:3306/test"
  3. 擷取Connection對象,該對象是JDBC封裝的資料庫連接對象,只有建立此對象後,才可以對資料執行相關操作。DriverManager.getConnection(url, username, password);

     如下為JDBC核心API的五個介面:

 

 

例:mysql資料表格式:

增加操作的核心代碼:

try{    Class.forName("com.mysql.jdbc.Driver");    String url="jdbc:mysql://localhost:3306/student";    Connection con=DriverManager.getConnection(url, "root", "");    String sql="insert into tb_books(name, price, bookCount, author) values(?,?,?,?)";    PreparedStatement ps=con.prepareStatement(sql);    ps.setString(1, book.getName());    ps.setDouble(2, book.getPrice());    ps.setInt(3, book.getBookCount());    ps.setString(4, book.getAuthor());    int row = ps.executeUpdate();    if(row>0){        out.print("成功添加"+row+"  行資料");    }    ps.close();    con.close();}catch(Exception e){    out.print("添加失敗!");    e.printStackTrace();}

查詢操作的核心代碼:

 

        try{            Class.forName("com.mysql.jdbc.Driver");            String url="jdbc:mysql://localhost:3306/student";            Connection con=DriverManager.getConnection(url, "root", "");            Statement statement=con.createStatement();            String sql="select * from tb_books";            ResultSet rs=statement.executeQuery(sql);            ArrayList<Book> list=new ArrayList<Book>();            while(rs.next()){                Book book=new Book();                book.setName(rs.getString("name"));                book.setPrice(rs.getDouble("price"));                book.setBookCount(rs.getInt("bookCount"));                book.setAuthor(rs.getString("author"));                list.add(book);            }            request.setAttribute("list", list);            rs.close();            con.close();        }         catch(ClassNotFoundException e){            e.printStackTrace();        }        catch(SQLException e){            e.printStackTrace();        }

修改資料的核心代碼:

 

try{            Class.forName("com.mysql.jdbc.Driver");            String url="jdbc:mysql://localhost:3306/student";            Connection con=DriverManager.getConnection(url, "root", "");            String sql="update tb_books set bookCount=? where name=?";            PreparedStatement ps=con.prepareStatement(sql);            ps.setInt(1, bookCount);            ps.setString(2, name);            ps.executeUpdate();            ps.close();con.close();        }        catch(Exception e){            e.printStackTrace();        }

對資料的其他動作類似。

 

      

Mysql的JDBC

聯繫我們

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