java H2資料庫使用並實現增刪改查功能

來源:互聯網
上載者:User

標籤:資料庫   h2   增刪改查   crud   java   

原文:java H2資料庫使用並實現增刪改查功能 代碼:http://www.zuidaima.com/share/1550463278058496.htm

java H2資料庫使用並實現增刪改查功能

package com.zuidaima.h2sql.test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class MainClass {/** * @param args         * @author www.zuidaima.com */public static void main(String[] args) {Statement stmt = null;Connection conn = null;try {Class.forName("org.h2.Driver");conn = DriverManager.getConnection("jdbc:h2:tcp://www.zuidaima.com:5435/mem:DBTest", "Guest","123456");// add application code herestmt = conn.createStatement();//String createSql = "create table product(product_id integer not null,title varchar2(30) not null,type_id integer not null,info varchar2(80),price number(16,2) not null,constraint product_pk primary key (product_id),constraint product_fk foreign key(type_id) references item(type_id)))";//stmt.executeUpdate(createSql);// 添加一條語句// String insertSql =// "insert into UserInfo values('劉尚','www.zuidaima.com','888888','true',15827288756,15827288756)";// stmt.executeUpdate(insertSql);// 刪除一條記錄// String deleteSql = "delete from USERINFO where UID = '劉尚1'";// stmt.executeUpdate(deleteSql);// 修改一條語句// String UpdateSql =// "UPDATE USERINFO  SET IP='www.zuidaima.com' ,  LINKID='11111' , ACTION='FALSE' ,  UPTIME=2121231 ,DOWNTIME=2121212 WHERE UID='劉尚'";// stmt.executeUpdate(deleteSql);ResultSet rs = stmt.executeQuery("SELECT * FROM UserInfo "); while (rs.next()) { System.out.println("Uid:" + rs.getString("Uid") + "\tIp:" + rs.getString("Ip") + "\tLinkId:" + rs.getString("LinkId") + "\tAction:" + rs.getBoolean("Action") + "\tUpTime:" + rs.getLong("UpTime") + "\tDownTime:" + rs.getLong("DownTime")); }} catch (Exception e) {e.printStackTrace();} finally {try {if (stmt != null) {stmt.close();stmt = null;}if (conn != null) {conn.close();conn = null;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

標籤: 資料庫 H2 增刪改查 CRUD java話題: 資料庫相關 入門教程 Web開發

相關文章

聯繫我們

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