Download and install Java SE 7 from
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Download and install NetBeans IDE 7 from
https://netbeans.org/downloads/7.4/
Open NetBeans IDE
Go to the Projects window. Tab on Services
Expand the Databases tree node
Right-click on Java DB and select Start
Server
Right-click on Java DB and select Create
Database. Key-in the following text:
Database Name: db1
User Name: db1username
Password: db1password
Confirm Password: db1password
Database Location:
C:\Users\yourname\.netbeans-derby
Click OK button
Right-click on jdbc:derby://localhost:1527/db1
[db1username on DB1USERNAME]
and select Properties. You will see the
following information:
Database URL: jdbc:derby://localhost:1527/db1
Driver: org.apache.derby.jdbc.ClientDriver
Schema: DB1USERNAME
User: db1username
Click Close button to close the database Properties
window
Right-click on jdbc:derby://localhost:1527/db1
and select Connect
Expand the “jdbc:derby://localhost:1527/db1”
tree node
Expand the DB1USERNAME tree node
Right-click on Tables and select Create
Table. Key-in the following information using the Create Table Dialog:
Table name: customer
Add the following columns to the customer
table:
customer_id, type: INTEGER, contraints:
Primary key & Unique & Index
customer_name, type: VARCHAR, size: 1000,
contraints: Null
customer_home_address, type: VARCHAR, size:
1000, contraints: Null
customer_mobile_phone_number, type: VARCHAR,
size: 1000, contraints: Null
customer_email_address, type: VARCHAR,
size: 1000, contraints: Null
Click OK button to close Create Table
window
Expand the Tables tree node
Right-click on CUSTOMER tree node, select “View
Data” to add table data using the SQL editor
Go to the CUSTOMER table middle window
toolbar, click on “Insert Record(s) (Alt+I)” icon
Insert the following Customer table data:
customer_id, customer_name,
customer_home_address, customer_mobile_phone_number, customer_email_address
1, name1, address1, mpnum1, email1
2, name2, address2, mpnum2, email2
3, name3, address3, mpnum3, email3
Click OK button to close Insert Record(s)
window
Create a Java Application project. Name the
project as prjJavaDBDerby1
Create a Java package. Name the package as test.
Create a new Java Class under test package. Name the Java Class as ReadDbTableData.java
The code for the ReadDbTableData.java file
content is as follows:
--
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ReadDbTableData
{
public static void main(String[]
args) {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
} catch (ClassNotFoundException
e) {
System.out.println(e);
}
try {
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/db1", "db1username", "db1password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM
db1username.customer");
while (rs.next()) {
int customer_id = rs.getInt("customer_id");
String customer_name = rs.getString("customer_name");
String customer_home_address = rs.getString("customer_home_address");
String customer_mobile_phone_number = rs.getString("customer_mobile_phone_number");
String customer_email_address = rs.getString("customer_email_address");
System.out.println(customer_id + ",
" +
customer_name
+ ", " +
customer_home_address
+ ", " +
customer_mobile_phone_number
+ ", " +
customer_email_address
);
}
} catch (SQLException
e) {
System.err.println(e);
}
}
}
--
Right-click on the project, select
Properties.
On the left window, select Libraries.
Click on “Add Library…” button.
Select “Java DB Driver” library. Click on
Add Library button to add this JAR library.
Click OK to close Project Properties window
Right-click on the ReadDbTableData.java file window and
select Run File
You should be getting the following output:
--
1, name1, address1, mpnum1, email1
2, name2, address2, mpnum2, email2
3, name3, address3, mpnum3, email3
BUILD SUCCESSFUL (total time: 0 seconds)
--
No comments:
Post a Comment