當前位置:首頁 » 數據倉庫 » java內嵌資料庫
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java內嵌資料庫

發布時間: 2022-09-21 05:24:50

❶ java寫的帶資料庫的程序如何在沒安裝資料庫的系統上運行

你的問題應該從兩個方面進行分析
第一、使用java內嵌式資料庫
在程序中使用
Derby和HsqlDB等java內嵌式資料庫,只需在程序中將資料庫配置好即可,是程序專用的。
可以達到你所說的java資料庫程序不需要配置在任何機器上運行。
但是,內嵌式資料庫的處理能力很低,只適合小數據量的程序使用。
第二、使用Oracle、MSSql、MySql等資料庫
首先這些資料庫軟體需要安裝,即使不在本機安裝也需要在一台可以訪問到的機器上安裝,然後使用jdbc訪問。
如果程序安裝在已經安裝這些資料庫的機器上,你只需要將程序的資料庫訪問指向本機即可。
但是,在本機安裝資料庫的機器很少,大部分都是訪問網路上的資料庫,這就需要你引入配置文件。
在配置文件中進行參數配置。當然,你可以寫一些默認配置,當實際環境與默認配置不同時就需要修改默認配置。
所以說不用配置的程序其實是符合你默認配置的程序,你需要根據不同的系統或環境組織自己的默認配置文件。
沒有萬能的東西,只能是適應大部分,而且需要你在編寫程序時提前考慮針對各種運行環境的變化。
有問題請留言吧

❷ 怎麼使用JAVA連接資料庫

1、首先我們先建好資料庫,然後建立好程序的目錄,因為是適用於初學者的,所以就建立一個簡單的java project,如圖。

❸ 用java怎樣把數據存到資料庫中

只能寫個大概的,要寫數據到資料庫中,先得在資料庫中建庫,庫里建表,表裡建欄位,然後java里建立資料庫連接,用SQL語言寫數據到表中的欄位
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=資料庫名"; //7.0、2000
String url="jdbc:sqlserver://localhost:1433;DatabaseName=資料庫名"; //2005
Connection conn=null;
conn= DriverManager.getConnection(url,用戶名,密碼);
PreparedStatement pst=null;
pst=conn.prepareStatement("Insert Into grade(表名) Values (?)");
pst.setInt(1,你要寫的整弄數據);
//pst.setString(2,你要寫的字元串數據);
pst.addBatch();
pst.executeBatch();

❹ Java DB是什麼

它卻是一個先進的全事務處理的基於Java技術的資料庫,它支持各類開放標准、觸發器和存儲程序。Java DB可以客戶端伺服器模式使用,也可以直接嵌入到一個Java應用程序中。在這些場合,Java DB都可以在同樣的Java虛擬機(JVM)中運行,這就無需在應用程序之外單獨購買、下載、安裝或管理這個資料庫。對於選擇在生產中採用Java DB的客戶,Sun將提供支持服務。 Java DB:Java 6 里的資料庫新安裝了 JDK 6 的程序員們也許會發現,除了傳統的 bin、jre 等目錄,JDK 6 新增了一個名為 javadb 的目錄。這便是 Java 6 的新成員:Java DB。這是一個純 Java 實現、開源的資料庫管理系統(DBMS),源於 Apache 軟體基金會(ASF)名下的項目 Derby。它只有 3MB 大小,對比動輒上 G 的資料庫來說可謂袖珍。但這並不妨礙 Derby 功能齊備,支持幾乎大部分的資料庫應用所需要的特性。更難能可貴的是,依託於 ASF 強大的社區力量,Derby 得到了包括 IBM 和 Sun 等大公司以及全世界優秀程序員們的支持。這也難怪 Sun 公司會選擇其 10.2.2 版本納入到 JDK 6 中,作為內嵌的資料庫。這就好像為 JDK 注入了一股全新的活力:Java 程序員不再需要耗費大量精力安裝和配置資料庫,就能進行安全、易用、標准、並且免費的資料庫編程。

❺ 怎樣在普通java項目中嵌入sqlite資料庫

只要導入jar包就行了,使用的時候載入class(即Class.forName("org.sqlite.JDBC");)
然後可以用最原始的jdbc代碼去使用sqlite
比如創建連接:Connectionconn=DriverManager.getConnection("jdbc:sqlite:test.db");(其中test.db就是資料庫文件以及資料庫的名稱,這句話有兩個作用:1、如果不存在該資料庫則創建並返回連接;2、如果存在了資料庫,則直接返回連接)
代碼如下:


importjava.sql.*;

publicclassSQLiteJDBC
{
publicstaticvoidmain(Stringargs[])
{
Connectionc=null;
try{
//Class.forName載入class
Class.forName("org.sqlite.JDBC");
c=DriverManager.getConnection("jdbc:sqlite:test.db");
}catch(Exceptione){
System.err.println(e.getClass().getName()+":"+e.getMessage());
System.exit(0);
}
System.out.println("Openeddatabasesuccessfully");
}
}

創建表
importjava.sql.*;

publicclassSQLiteJDBC
{
publicstaticvoidmain(Stringargs[])
{
Connectionc=null;
Statementstmt=null;
try{
//Class.forName載入class
Class.forName("org.sqlite.JDBC");
//DriverManager.getConnection創建連接
c=DriverManager.getConnection("jdbc:sqlite:test.db");
System.out.println("Openeddatabasesuccessfully");
stmt=c.createStatement();
//sql創建表語句
Stringsql="CREATETABLECOMPANY"+
"(IDINTPRIMARYKEYNOTNULL,"+
"NAMETEXTNOTNULL,"+
"AGEINTNOTNULL,"+
"ADDRESSCHAR(50),"+
"SALARYREAL)";
//executeUpdate創建表
stmt.executeUpdate(sql);
stmt.close();
c.close();
}catch(Exceptione){
System.err.println(e.getClass().getName()+":"+e.getMessage());
System.exit(0);
}
System.out.println("Tablecreatedsuccessfully");
}
}

❻ 如何在java程序中引入neo4j資料庫

neo4j採納java語言開發,如果我們要在java程序中以內嵌方式應用neo4j,只需導入neo4j的對應包即可。
首先,我們來創建一個maven項目並改動pom.xml添加對neo4j的依附。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> <modelVersion>4.0.0</modelVersion> <groupId>neo4j-learn</groupId> <artifactId>neo4j-learn</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j</artifactId> <version>1.9.4</version> </dependency> </dependencies> </project>
然後,我們在項目中創建一個neo4j.properties(資料庫的配置文件)文件和一個java類(調用資料庫)。
neo4j.properties
# Default values for the low-level graph engine #neostore.nodestore.db.mapped_memory=25M #neostore.relationshipstore.db.mapped_memory=50M #neostore.propertystore.db.mapped_memory=90M #neostore.propertystore.db.strings.mapped_memory=130M #neostore.propertystore.db.arrays.mapped_memory=130M # Autoindexing # Enable auto-indexing for nodes, default is false #node_auto_indexing=true # The node property keys to be auto-indexed, if enabled #node_keys_indexable=name,age # Enable auto-indexing for relationships, default is false #relationship_auto_indexing=true # The relationship property keys to be auto-indexed, if enabled #relationship_keys_indexable=name,age # Keep logical logs, needed for online backups to work keep_logical_logs=true # Enable online backups to be taken from this database. online_backup_enabled=true # Uncomment and specify these lines for running Neo4j in High Availability mode. # ha.server_id is a unique integer for each instance of the Neo4j database in the cluster. # (as opposed to the coordinator instance IDs) # example: ha.server_id=1 #ha.server_id= # ha.coordinators is a comma-separated list (without spaces) of the host:port of where to # find one or more of the Neo4j coordinator servers. # Avoid localhost e to IP resolution issues on some systems. # example: ha.coordinators=localhost:2181,1.2.3.4:4321 #ha.coordinators=localhost:2181 # You can also, optionally, configure the ha.cluster_name. This is the name of the cluster this # instance is supposed to join. Accepted characters are alphabetical, numerical, dot and dash. # This configuration is useful if you have multiple Neo4j HA clusters managed by the same # Coordinator cluster. # Example: ha.cluster_name = my.neo4j.ha.cluster #ha.cluster_name = # IP and port for this instance to bind to to communicate data with the # other neo4j instances in the cluster. This is broadcasted to the other # cluster members, so different members can have different communication ports. # Optional if the members are on different machines so the IP is different for every member. #ha.server = localhost:6001 # The interval at which slaves will pull updates from the master. Comment out # the option to disable periodic pulling of updates. Unit is seconds. ha.pull_interval = 10 # The session timeout for the zookeeper client. Lower values make new master # election happen closer to the master loosing connection but also more sensitive # to zookeeper quorum hiccups. If experiencing master switches without reason # consider increasing this value. Unit is seconds #ha.zk_session_timeout = 5 # Amount of slaves the master will try to push a transaction to upon commit (default is 1). # The master will optimistically continue and not fail the transaction even if it fails to # reach the push factor. Setting this to 0 will increase write performance when writing # through master but could potentially lead to branched data (or loss of transaction) # if the master goes down. #ha.tx_push_factor=1 # Strategy the master will use when pushing data to slaves (if the push factor is greater than 0). # There are two options available "fixed" (default) or "round_robin". Fixed will start by # pushing to slaves ordered by server id (highest first) improving performance since the # slaves only have to cache up one transaction at a time. #ha.tx_push_strategy=fixed # Enable this to be able to upgrade a store from 1.4 -> 1.5 or 1.4 -> 1.6 #allow_store_upgrade=true # Enable this to specify a parser other than the default one. 1.5, 1.6, 1.7 are available #cypher_parser_version=1.6
java文件(neo4j示例文件改動而來)

❼ JAVA嵌入資料庫:用java代碼實現像資料庫表中插入信息,怎麼寫

Java程序向資料庫中插入數據,代碼如下:
//首先創建資料庫,(access,oracle,mysql,sqlsever)其中之一,其中access,sqlsever需要配置數據源(odbc);//然後再eclipse中創建類(ConnDb,Test,TestBean)ConnDb功能為連接資料庫,查詢,插入,刪除,修改數據的類,Test為含有main方法的測試類,TestBean為數據表中的欄位屬性及set,get方法//以下是ConnDb代碼:package db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;public class ConnDb {public Connection startConn(Connection conn){ try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:資料庫","用戶名", "密碼"); } catch (Exception e) { System.out.println("連接資料庫時出現錯誤"); } return conn; } public ArrayList executeQuery(String sql){ Connection conn = null; Statement stmt = null; ResultSet rs = null; ArrayList list = new ArrayList(); try { conn = startConn(conn); stmt = conn.createStatement(); rs = stmt.executeQuery(sql);//sql為sql語句例如"select * from 表名",從main方法中傳進來,這里用的是ArrayList 類將查詢結果存儲起來 while(rs.next()){ TestBean tb = new TestBean(); tb.setTid(rs.getString("tid")); tb.setTname(rs.getString("tname")); tb.setTinfo(rs.getString("tinfo")); list.add(tb); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeConn(rs,stmt,conn); } return list; } public void executeUpdate(String sql){ Connection conn = null; Statement stmt = null; try { conn = startConn(conn); stmt = conn.createStatement(); stmt.executeUpdate(sql); } catch (SQLException e) { System.out.println("修改,插入或者刪除資料庫數據時發生錯誤!"); }finally{ closeConn(stmt,conn); } } public void closeConn(ResultSet rs,Statement stmt,Connection conn){ try { if(rs != null){ rs.close(); } if(stmt != null){ stmt.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("關閉資料庫的時候發生錯誤!"); } } public void closeConn(Statement stmt,Connection conn){ try { if(stmt != null){ stmt.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("關閉資料庫的時候發生錯誤!"); } }}

❽ java寫的帶資料庫的程序如何在沒安裝資料庫的系統上運行

你的問題應該從兩個方面進行分析
第一、使用java內嵌式資料庫
在程序中使用 Derby和HSQLDB等java內嵌式資料庫,只需在程序中將資料庫配置好即可,是程序專用的。
可以達到你所說的java資料庫程序不需要配置在任何機器上運行。
但是,內嵌式資料庫的處理能力很低,只適合小數據量的程序使用。
第二、使用Oracle、MSSql、MySql等資料庫
首先這些資料庫軟體需要安裝,即使不在本機安裝也需要在一台可以訪問到的機器上安裝,然後使用jdbc訪問。
如果程序安裝在已經安裝這些資料庫的機器上,你只需要將程序的資料庫訪問指向本機即可。
但是,在本機安裝資料庫的機器很少,大部分都是訪問網路上的資料庫,這就需要你引入配置文件。
在配置文件中進行參數配置。當然,你可以寫一些默認配置,當實際環境與默認配置不同時就需要修改默認配置。

所以說不用配置的程序其實是符合你默認配置的程序,你需要根據不同的系統或環境組織自己的默認配置文件。
沒有萬能的東西,只能是適應大部分,而且需要你在編寫程序時提前考慮針對各種運行環境的變化。

有問題請留言吧

❾ java自帶的資料庫怎麼用,能不能給一段運行的通的代碼.(包括import)

你是指javaDB Derby嗎?Derby資料庫是一個純用Java實現的內存資料庫,屬於Apache的一個開源項目。由於是用Java實現的,所以可以在任何平台上運行;另外一個特點是體積小,免安裝,只需要幾個小jar包就可以運行了。
Derby資料庫有兩種運行模式:
1) 內嵌模式。Derby資料庫與應用程序共享同一個JVM,通常由應用程序負責啟動和停止,對除啟動它的應用程序外的其它應用程序不可見,即其它應用程序不可訪問它;
2) 網路模式。Derby資料庫獨佔一個JVM,做為伺服器上的一個獨立進程運行。在這種模式下,允許有多個應用程序來訪問同一個Derby資料庫。
在Apache上,Derby有4種發布包,這里以bin發布包為例。bin發布包中含有包含了執行derby資料庫工具、設置derby資料庫環境的腳本文件、Demo、jar文件等。

希望幫到你……

1、 安裝Derby資料庫
只需要從Derby官方網站下載Derby的zip或者tar包,解壓就可以了。這里以db-derby-10.4.1.3-bin版本為例,解壓後得到以下目錄:
1) bin目錄,包含了一些工具腳本和設備環境的腳本;
2) demo目錄,包含了一些實常式序;
3) docs目錄,包含了Derby的文檔;
4) javadoc目錄,包含了Derby的API文檔;
5) lib目錄,包含了Derby資料庫的jar文件;
6) test目錄,Derby的一些測試jar包;
2、 使用Derby腳本
Derby提供了幾個腳本來操作Derby資料庫,在使用這些腳本前,你必須先設置好Derby的運行環境。
下面的例子都是在命令行下設置環境變數,這些設置都是臨時的,如果你新開一個命令行窗口,則需要重新設置一遍,如果想要使環境變數永久生效,可以在我的電腦中進行設置。
首先設置好DERBY_HOME這個環境變數,為DERBY_HOME指定你的derby目錄,假如你的derby解壓到了E:\ db-derby-10.4.1.3-bin目錄下,則可以在命令行中如下設置:
set DERBY_HOME=E:\ db-derby-10.4.1.3-bin
將DERBY_HOME\bin目錄添加到PATH環境變數中:
set path=%DERBY_HOME%\bin;%PATH%
這樣可以簡化你稍後在命令行中的輸入,否則你每次都必須使用腳本的全路徑或者你必須到DERBY_HOME\bin目錄中才能執行腳本。
最後需要Derby的jar包添加到classpath環境變數中,在DERBY_HOME%\bin目錄中提供了幾個腳本用於設置classpath,以簡化你手工在classpath中添加jar包的麻煩:
1) setEmbeddedCP。當使用內嵌模式來運行Derby時,可以使用該腳本來設置。該腳本將derby.jar和derbytools.jar添加到環境變數中;
2) setNetworkServerCP。當使用網路模式來運行Derby時,用該腳本來設置Derby服務端的classpath變數。該腳本將derbynet.jar添加到環境變數中;
3) setNetworkClientCP。當使用網路模式來運行Derby時,用該腳本來設置Derby客戶端的classpath變數。該腳本將derbyclient.jar和derbytools.jar添加到環境變數中。
一般只有當你通過derbyrun.jar來運行Derby工具時才會使用這些腳本。
Derby提供了三個工具腳本:1)sysinfo;2)ij;3)dblook。運行這三個腳本時,如果你沒有設置classpath環境變數,這些腳本會自動進行設置。
1) sysinfo
使用sysinfo可以顯示你的Java環境信息和Derby的版本信息。使用方法就是在命令行下直接輸入:
sysinfo.bat
2) dblook
使用dblook可以將全部或者部分資料庫的DDL定義導出到控制台或者文件中。使用方法:
dblook.bat –d <sourceDBUrl> [Options]
3) ij
使用ij工具來進行資料庫交互,執行SQL腳本,如查詢、增刪改、創建表等等。在命令行下輸入:
ij.bat
即可啟動ij工具,然後就可以開始執行SQL腳本了。當要退出ij工具時,在命令行下輸入
exit;
即可。
3、 使用ij腳本
1) 運行內嵌模式的Derby資料庫
在命令行中輸入ij.bat後啟動ij工具。然後通過如下命令創建資料庫,並與資料庫創建連接:
connect 『jdbc:derby:firstdb;create=true』;
通過connect命令可以與指定資料庫創建連接,通過一個JDBC URL來指定與哪個資料庫創建連接。ij命令是不區分大小寫的。
參數中jdbc:derby是Derby資料庫的驅動協議;firstdb是資料庫命,由於沒有指定路徑,資料庫將會被創建在當前你命令行下所在的目錄下;create=true表示如果資料庫不存在,則創建該資料庫;「;」是ij命令的終止符。
當資料庫創建成功時,Derby會在當前你命令行下所在的目錄下創建一個與資料庫命一致(這里是firstdb)的目錄,其中存放了資料庫的文件。
與資料庫連接上後,就可以開始執行SQL腳本了,如創建一個表格:
create table firsttable(id int primary key, name varchar(20));
然後插入記錄:
insert into firsttable values(1, 『Hotpepper』);
也可以執行查詢:
select * from firsttable;
也可以通過run命令來執行sql文件:
run 'E:\derby\demo\programs\toursdb\ToursDB_schema.sql';
最後通過exit;來退出ij工具。
你可以在當前你命令行下所在的目錄中找到一個derby.log的日誌文件,derby在其中記錄的資料庫啟動、關閉的信息。
2) 運行網路模式的Derby資料庫
這種模式下,需要使用兩個控制台窗口,一個用於啟動Derby資料庫服務端,另一個做為訪問Derby資料庫的客戶端。
可以通過DERBY_HOME\bin目錄下的startNetworkServer.bat來啟動Derby資料庫服務端,只需要在命令行中輸入:
startNetworkServer.bat
資料庫就啟動了,啟動成功會在控制台輸出如下信息:
已使用基本伺服器安全策略安裝了安全管理程序。
Apache Derby Network Server - 10.4.1.3 - (648739) 已啟動並且已准備好 2008-09-06
00:38:12.540 GMT 時在埠 1527 上接受連接
在另一個控制台使用ij命令訪問Derby資料庫服務端,在輸入ij.bat啟動ij工具後,通過如下命令建立與服務端的連接,並創建一個資料庫:
connect 'jdbc:derby://localhost:1527/seconddb;create=true';
參數中的資料庫命部分和內嵌模式不同,這里使用了「//localhost:1527/」,訪問網路模式的URL需要指定伺服器的IP地址和埠,其它的就和內嵌模式一樣了。
與服務端連接上後,就可以開始執行SQL腳本了,如創建一個表格:
create table firsttable(id int primary key, name varchar(20));
然後插入記錄:
insert into firsttable values(1, 『Hotpepper』);
也可以執行查詢:
select * from firsttable;
也可以通過run命令來執行sql文件:
run 'E:\derby\demo\programs\toursdb\ToursDB_schema.sql';
最後通過exit;來退出ij工具
4、 在Java應用程序中訪問Derby資料庫
使用Java代碼訪問Derby資料庫與訪問其它資料庫的區別如下:
1) JDBC驅動的不同;
2) 資料庫連接URL的不同;
3) 在訪問內嵌模式資料庫時,需要顯示關閉資料庫。
下面分別實例訪問內嵌模式和網路模式Derby資料庫的代碼
1) 訪問內嵌模式Derby資料庫
String driver = 「org.apache.derby.jdbc.EmbeddedDriver」;
String url = 「jdbc:derby:firstdb;create=true」;
Connection conn;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
}catch(Exception e) {
……
}finally {
……
DriverManager.getConnection("jdbc:derby:;shutdown=true");
}
建立好連接後,其它的數據操作,如查詢、更新數據都和其它資料庫一樣,這里不詳述。有一點需要注意,通過Java應用程序訪問內嵌模式Derby資料庫時,應用程序有責任需要在程序結束時關閉Derby資料庫,如上面代碼finally中的
DriverManager.getConnection("jdbc:derby:;shutdown=true");
shutdown參數用於關閉Derby資料庫,如果url中指定了資料庫命,則只會關閉指定的資料庫,而不會關閉整個Derby資料庫。資料庫關閉成功時,Derby會拋出一個錯誤碼為XJ015和一個08006的異常表示關閉成功,應用程序可以不處理這兩個異常。
2) 訪問網路模式Derby資料庫
網路模式和內嵌模式的不同出在於:
A. 資料庫連接URL的不同;
B. 應用程序退出時無效關閉Derby資料庫;
C. 資料庫驅動的不同;
String driver = 「org.apache.derby.jdbc.ClientDriver」;
String url = 「jdbc:derby: //localhost:1527/firstdb;create=true」;
Connection conn;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
}catch(Exception e) {
……
}
由於網路模式下,Derby資料庫做為一個獨立運行的資料庫,可以被多個應用程序所訪問,所以應用程序在運行結束時不應該關閉Derby資料庫。

❿ java中嵌入.sql文件有什麼用連接資料庫不是可以實現存取嗎,為什麼還要導入.sql文件呢

導入sql文件都是會用到半自動映射框架的,使得業務處理更靈活。