Connect with Apache Derby Network Server
Apache Derby, an is an open source relational database implemented entirely in Java.
Refer http://db.apache.org/derby/
Refer http://db.apache.org/derby/
- Start Apache Derby Network Server
Run "startNetworkServer" Batch file to start Apache Derby Network Server.
(You can find this batch file at "C:\Program Files\Java\jdk1.7.0_21\db\bin\")
cmd prompt appears like this
(Server runs on port 1527 and i created database in E: drive refer DerbyTest.java)
Tue Jun 18 11:16:47 IST 2013 : Security manager installed using the Basic server
security policy.
Tue Jun 18 11:16:59 IST 2013 : Apache Derby Network Server - 10.8.2.2 - (1181258
) started and ready to accept connections on port 1527
- create a java class to connect to database and get records
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class DerbyTest {
private Connection connect = null;
private Statement statement = null;
private ResultSet resultSet = null;
public DerbyTest() throws Exception {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
connect = DriverManager
.getConnection("jdbc:derby:e:/mydb");
PreparedStatement statement = connect
.prepareStatement("SELECT * FROM MYTABLE");
resultSet = statement.executeQuery();
while (resultSet.next()) {
String id = resultSet.getString("ID");
String name = resultSet.getString("NAME");
System.out.println("ID: " + id);
System.out.println("NAME: " + name);
}
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void close() {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
public static void main(String[] args) throws Exception {
DerbyTest dao = new DerbyTest();
}
}
Compile DerbyTest.java
E:/>javac DerbyTest.java
Run
E:/>java DerbyTest
You will Get output as
table ctreated.....
records inserted sucessfully ......ID: 10
NAME: java
ID: 20
NAME: derby
ID: 30
NAME: oracle
Run "startNetworkServer" Batch file to start Apache Derby Network Server.
(You can find this batch file at "C:\Program Files\Java\jdk1.7.0_21\db\bin\")
cmd prompt appears like this
(Server runs on port 1527 and i created database in E: drive refer DerbyTest.java)
Compile DerbyTest.java
Run
You will Get output as
(You can find this batch file at "C:\Program Files\Java\jdk1.7.0_21\db\bin\")
cmd prompt appears like this
(Server runs on port 1527 and i created database in E: drive refer DerbyTest.java)
Tue Jun 18 11:16:47 IST 2013 : Security manager installed using the Basic server security policy. Tue Jun 18 11:16:59 IST 2013 : Apache Derby Network Server - 10.8.2.2 - (1181258 ) started and ready to accept connections on port 1527
- create a java class to connect to database and get records
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class DerbyTest { private Connection connect = null; private Statement statement = null; private ResultSet resultSet = null; public DerbyTest() throws Exception { try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); connect = DriverManager .getConnection("jdbc:derby:e:/mydb"); PreparedStatement statement = connect .prepareStatement("SELECT * FROM MYTABLE"); resultSet = statement.executeQuery(); while (resultSet.next()) { String id = resultSet.getString("ID"); String name = resultSet.getString("NAME"); System.out.println("ID: " + id); System.out.println("NAME: " + name); } } catch (Exception e) { throw e; } finally { close(); } } private void close() { try { if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connect != null) { connect.close(); } } catch (Exception e) { } } public static void main(String[] args) throws Exception { DerbyTest dao = new DerbyTest(); } }
E:/>javac DerbyTest.java
E:/>java DerbyTest
table ctreated..... records inserted sucessfully ......ID: 10 NAME: java ID: 20 NAME: derby ID: 30 NAME: oracle
No comments:
Post a Comment