java 使用java代码创建derby数据库、表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13695064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create derby database, tables using java code
提问by Komal Rathi
I downloaded derby in C:\ directly and set the DERBY_INSTALL and CLASSPATH variables:
我直接在 C:\ 下载了 derby 并设置了 DERBY_INSTALL 和 CLASSPATH 变量:
C:\>set DERBY_INSTALL=C:\Apache\db-derby-10.9.1.0-bin
C:\>set CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar;.
C:\> cd %DERBY_INSTALL%\bin
C:\db-derby-10.9.1.0-bin\bin>java org.apache.derby.tools.ij
ij version 10.9
ij>
So it shows ij> and I am able to create a database using command line SQL statements. But I have to create the database and its tables using a Java code. So I the source code below but I dont know where to paste it and how to run it. When I pasted it in the db-derby-10.9.1.0-bin>bin folder and tried to run it within that folder using 'java MainClass komaldb C:\' it shows could not find or load main class MainClass. I am not sure where do I have to run the java program from.
所以它显示 ij> 并且我能够使用命令行 SQL 语句创建一个数据库。但我必须使用 Java 代码创建数据库及其表。所以我在下面的源代码,但我不知道在哪里粘贴它以及如何运行它。当我将它粘贴到 db-derby-10.9.1.0-bin>bin 文件夹中并尝试使用“java MainClass komaldb C:\”在该文件夹中运行它时,它显示无法找到或加载主类 MainClass。我不确定我必须从哪里运行 java 程序。
Java Source Code:
Java源代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class MainDB {
static Connection conn;
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Usage: java JavaDBDemo <Name> <Address>");
System.exit(1);
}
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName = "AddressBookDB";
String connectionURL = "jdbc:derby:" + dbName + ";create=true";
String createString = "CREATE TABLE ADDRESSBOOKTbl (NAME VARCHAR 32) NOT NULL,
ADDRESS VARCHAR(50) NOT NULL)";
Class.forName(driver);
conn = DriverManager.getConnection(connectionURL);
Statement stmt = conn.createStatement();
stmt.executeUpdate(createString);
PreparedStatement psInsert = conn
.prepareStatement("insert into ADDRESSBOOKTbl values (?,?)");
psInsert.setString(1, args[0]);
psInsert.setString(2, args[1]);
psInsert.executeUpdate();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt2.executeQuery("select * from ADDRESSBOOKTbl");
System.out.println("Addressed present in your Address Book\n\n");
int num = 0;
while (rs.next()) {
System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address"
+ rs.getString(2));
}
rs.close();
}
}
回答by Bryan Pendleton
It seems like you are just getting started with Java.
您似乎才刚刚开始使用 Java。
Use of Derby assumes that you are already quite comfortable with the basics of Java programming.
使用 Derby 假定您已经非常熟悉 Java 编程的基础知识。
I recommend downloading and running some Java tutorials, such as the ones available here: http://docs.oracle.com/javase/tutorial/
我建议下载并运行一些 Java 教程,例如此处提供的教程:http: //docs.oracle.com/javase/tutorial/
After you become more comfortable with writing and running Java programs, you can start to learn about Derby. I recommend downloading the Derby tutorial available here: http://db.apache.org/derby/papers/DerbyTut/index.html
在您更加熟悉编写和运行 Java 程序后,您可以开始学习 Derby。我建议在此处下载 Derby 教程:http: //db.apache.org/derby/papers/DerbyTut/index.html