java db 使用_javaDB简单使用笔记

java db 使用_javaDB简单使用笔记Javadb 学习环境变量设置 DERBY HOME D ProgramFiles Environment javadbpath JAVA HOME bin path CATALINA HOME bin ANT HOME bin ANT HOME bin C ProgramFiles IDMComputerS UltraEdit JAVA HOM

Javadb学习

环境变量设置:

DERBY_HOME=D:\ProgramFiles\Environment\javadb

path=.;%JAVA_HOME%/bin;%path%;%CATALINA_HOME%/bin;%ANT_HOME%/bin;%ANT_HOME%/bin;C:\Program Files\IDM Computer Solutions\UltraEdit\;%JAVA_HOME%\jre\lib;%DERBY_HOME%\bin

classpath=.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\bin;%JAVA_HOME%\jre\lib;%DERBY_HOME%\lib\derby.jar;%DERBY_HOME%\lib\derbyclient.jar;%DERBY_HOME%\lib\derbynet.jar;%DERBY_HOME%\lib\derbyrun.jar

开启Javadb服务:

java -jar %derby_home%\lib\derbyrun.jar server start

关闭javadb服务

java -jar %derby_home%\lib\derbyrun.jar server shutdown

ij

ij> exit;  退出

用ij连接数据库javadb

Creating a Derby database and running SQL statementsNow, you will use the Derby ij tool to load the Derby database engine. You will use theDerby embedded driver to create and connect to the firstdb database. You will alsouse a few basic SQL statements to create and populate a table.1. Run the Derby ij tool.If you included the DERBY_HOME/bin directory in your PATH environment variable,type:ijOtherwise, you can use the java command to start the ij tool.OperatingSystemCommandUNIX(Korn Shell)java -jar $DERBY_HOME/lib/derbyrun.jar ijij version 10.5Windows java -jar %DERBY_HOME%\lib\derbyrun.jar ijij version 10.52. Create the database and open a connection to the database using the embeddeddriver.CONNECT ‘jdbc:derby:firstdb;create=true’;Description of connection command:connectThe ij command to establish a connection to a database. The Derbyconnection URL is enclosed in single quotation marks. An ij command canbe in either uppercase or lowercase.jdbc:derby:The JDBC protocol specification for the Derby driver.firstdbThe name of the database. The name can be any string. Because nofilepath is specified, the database is created in the default working directory(DERBYTUTOR).;create=trueThe Derby URL attribute that is used to create a database. Derby does nothave an SQL create database command.;Getting Started with Java DB23The semicolon is the ij command terminator.3. Create a table with two columns using standard SQL.CREATE TABLE FIRSTTABLE(ID INT PRIMARY KEY,NAME VARCHAR(12));0 rows inserted/updated/deleted4. Insert three records.INSERT INTO FIRSTTABLE VALUES(10,’TEN’),(20,’TWENTY’),(30,’THIRTY’);3 rows inserted/updated/deleted5. Perform a simple select of all records in the table.SELECT * FROM FIRSTTABLE;ID |NAME————————10 |TEN20 |TWENTY30 |THIRTY3 rows selected6. Perform a qualified select of the record with column ID=20.SELECT * FROM FIRSTTABLEWHERE ID=20;ID |NAME————————20 |TWENTY1 row selected7. Optional: Create and populate additional tables and other schema objects.a. Load the SQL script ToursDB_schema.sql.run ‘ToursDB_schema.sql’;ij> …CREATE TABLE AIRLINES(AIRLINE CHAR(2) NOT NULL ,AIRLINE_FULL VARCHAR(24),BASIC_RATE DOUBLE PRECISION,…0 rows inserted/updated/deleted… Other output messages not shown …b. Populate the tables with data by running the script loadTables.sql.run ‘loadTables.sql’;ij> run ‘loadCOUNTRIES.sql’;ij> insert into COUNTRIES values ( ‘Afghanistan’,’AF’,’Asia’);1 row inserted/updated/deletedij> insert into COUNTRIES values ( ‘Albania’,’AL’,’Europe’);1 row inserted/updated/deleted… Other output messages not shown …8. Exit the ij tool.exit;You should be returned to the DERBYTUTOR directory.9. Browse the most

Activity 2: Run SQL using the client driver

java -jar %DERBY_HOME%\lib\derbyrun.jar server start

换一个dos窗口

java -jar %DERBY_HOME%\lib\derbyrun.jar ij

CONNECT ‘jdbc:derby://localhost:1527/seconddb;create=true’;

备注区别:CONNECT ‘jdbc:derby:firstdb;create=true’;

CREATE TABLE SECONDTABLE(ID INT PRIMARY KEY,NAME VARCHAR(14));0 rows inserted/updated/deleted

INSERT INTO SECONDTABLE VALUES(100,’ONE HUNDRED’),(200,’TWO HUNDRED’),(300,’THREE HUNDRED’);3 rows inserted/updated/deleted

SELECT * FROM SECONDTABLE;ID |NAME————————100 |ONE HUNDRED200 |TWO HUNDRED300 |THREE HUNDRED3 rows selected

Exit ij.

java -jar %DERBY_HOME%\lib\derbyrun.jar servershutdown

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

import org.apache.derby.jdbc.EmbeddedDriver;

public class JavaDBTest {

public static void main(String[] args) throws Exception {

String driver = “org.apache.derby.jdbc.EmbeddedDriver”;

String url = “jdbc:derby:”;

String facetDir = “aaa”;

Class.forName(driver).newInstance();

Connection connection = DriverManager.getConnection(url + facetDir+ “;create=true”);

connection.setAutoCommit(true);

Statement stmt = connection.createStatement();

String creatSql=”CREATE TABLE testDB (” + “facetId INTEGER PRIMARY KEY,” + ” name VARCHAR(256))”;

stmt.executeUpdate(creatSql);

stmt.executeUpdate(“INSERT INTO testDB(facetId, name) ” + “VALUES(1, ‘123’)”);

stmt.executeUpdate(“INSERT INTO testDB(facetId, name) ” + “VALUES(2, ‘123’)”);

stmt.executeUpdate(“INSERT INTO testDB(facetId, name) ” + “VALUES(3, ‘123’)”);

System.out.println(stmt.executeQuery(“select * from testDB”).toString());

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/214046.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午5:06
下一篇 2026年3月18日 下午5:07


相关推荐

  • java向上取整和向下取整,万字长文!

    java向上取整和向下取整,万字长文!一面:70分钟突击电话面试正思考着项目功能模块,阿里面试官打来了电话,开始了阿里一面。阿里面试官自我介绍,介绍了5分钟左右,部门的情况,主要的业务提问开始会哪些操作系统Linux会一点说一下操作指令,怎么看cpu,看进程,看端口操作系统进程间通信追问了一个信号相关的问题,我不知道了。io多路复用,说一说面向切面编程,说一说那些场景说说面向切面编程给一个场景,有很多方法,找出耗时长的方法spring的@autowired的作用mybatis和hibernate的区别C,C

    2022年6月21日
    30
  • Manus 1.6 发布:Max 性能、移动开发和 Design View

    Manus 1.6 发布:Max 性能、移动开发和 Design View

    2026年3月15日
    2
  • adb操作命令详解及大全

    adb操作命令详解及大全adb是什么?:adb的全称为AndroidDebugBridge,就是起到调试桥的作用。通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具。adb的工作方式比较特殊,采用监听SocketTCP5554等端口的方式让ID

    2022年6月12日
    30
  • Linux生成静态库_linux生成静态库

    Linux生成静态库_linux生成静态库转自:https://blog.csdn.net/ddreaming/article/details/53096411一、动态库、静态库简介库是写好的现有的,成熟的,可以复用的代码。现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常。本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行。库有两种:静态库.a(win系统下是lib)和动态…

    2026年4月19日
    7
  • scrapy安装指南

    scrapy安装指南scrapy 安装指南

    2026年3月17日
    2
  • phpstorm2021 激活码【2021免费激活】

    (phpstorm2021 激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~2QQ4OQYW6M-eyJsaWNlb…

    2022年3月22日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号