Reputation: 4274
I downloaded derby in C:\ directly and set the DERBY_INSTALL and CLASSPATH variables:
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.
Java Source Code:
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();
}
}
Upvotes: 2
Views: 13316
Reputation: 16349
It seems like you are just getting started with Java.
Use of Derby assumes that you are already quite comfortable with the basics of Java programming.
I recommend downloading and running some Java tutorials, such as the ones available here: 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
Upvotes: 2