標籤:資料庫 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開發