Today i'm going tell you "how to make a java program with embedded Database in netbeans "
you guys are tired with *.accb sql ,datasource for bridging ,bla,bla,bla
its very easy to implement a databse with java program in netbeans
steps You should follow
Create a project
name it "Simpledatabse" or anything as you wish i am not goig to force you for putting my suggestion :)
Sorry guys i am always backward in my english ,so pls point me out if any grammatical mistake, i doing :)
trust me :) ,if you need more screen shot ,i will update this post by giving adding more screen shot :)
Step 1
------------
So you created project name with Simpledatabse
Step 2
------------
add JFrame form name it as you wish "Addform"
Step 3
--------------------------
Add two Jtextfielsd, a button
Jtextfield 1:- Change variable name :"txtxName"
Jtextfield 2:-Change variable name: "txtid"
jbutton 1:-change variable name "btnShow",Edit text name "Show"
Step 4
-------------------
Goto source code of Addform
import sql with this code
--------------------------------------------------
import java.sql.*;
-----------------------------------------------
copy paste this code in constructor
---------------------------------------------
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
}catch(ClassNotFoundException e){
System.out.println(e);
}
---------------------------------------------------------------------------
Now we are going to create a database with java derby
Go to windows→services→There you can see "Database" Right click on it "Create database"
Give
-----------------------------------
Name of databese :"simpledbb"
user: "asd"
pass: "asd"
Right click on icon named with "jdbc:derby://localhost:1527/simpledbb...."→Connect
Now goto App→Table→Right click on →"Create table "
------------------------
Table name: "Table1"
Click add column Name it "Name" and Type:"varchar" →ok
Click add column Name it "id" and Type:"number"→ok
→OK
Step 5
goto your Addform→double click on button→it will point to its source code
Now copy paste this code
---------------------------------------------------------------------------------------------
try{
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/simpledbb ","asd","asd");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM APP.Table1");
while (rs.next()) {
String s = rs.getString("Name");
int n = rs.getInt("Age");
String k=String.valueOf(n);
txtName.setText(s);
txtID.setText(k);
}
}catch(SQLException e){
System.err.println(e);
}
---------------------------------------------------------
Now you have noticed it from other type databases
with difference in these 2 lines
***************************************************
Line 1:Database Url
-------------
MS access
DriverManager.getConnection("jdbc:odbc:databasename");
Derby database
DriverManager.getConnection("jdbc:derby://localhost:1527/simpledbb,"username","password");
here we used derby
Line 2:Database Driver
-----------
MS ACCESS
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Derby database
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
you guys are tired with *.accb sql ,datasource for bridging ,bla,bla,bla
its very easy to implement a databse with java program in netbeans
steps You should follow
Create a project
name it "Simpledatabse" or anything as you wish i am not goig to force you for putting my suggestion :)
Sorry guys i am always backward in my english ,so pls point me out if any grammatical mistake, i doing :)
trust me :) ,if you need more screen shot ,i will update this post by giving adding more screen shot :)
Step 1
------------
So you created project name with Simpledatabse
Step 2
------------
add JFrame form name it as you wish "Addform"
Step 3
--------------------------
Add two Jtextfielsd, a button
Jtextfield 1:- Change variable name :"txtxName"
Jtextfield 2:-Change variable name: "txtid"
jbutton 1:-change variable name "btnShow",Edit text name "Show"
Step 4
-------------------
Goto source code of Addform
import sql with this code
--------------------------------------------------
import java.sql.*;
-----------------------------------------------
copy paste this code in constructor
---------------------------------------------
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
}catch(ClassNotFoundException e){
System.out.println(e);
}
---------------------------------------------------------------------------
Now we are going to create a database with java derby
Go to windows→services→There you can see "Database" Right click on it "Create database"
Give
-----------------------------------
Name of databese :"simpledbb"
user: "asd"
pass: "asd"
Right click on icon named with "jdbc:derby://localhost:1527/simpledbb...."→Connect
Now goto App→Table→Right click on →"Create table "
------------------------
Table name: "Table1"
Click add column Name it "Name" and Type:"varchar" →ok
Click add column Name it "id" and Type:"number"→ok
→OK
Step 5
goto your Addform→double click on button→it will point to its source code
Now copy paste this code
---------------------------------------------------------------------------------------------
try{
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/simpledbb ","asd","asd");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM APP.Table1");
while (rs.next()) {
String s = rs.getString("Name");
int n = rs.getInt("Age");
String k=String.valueOf(n);
txtName.setText(s);
txtID.setText(k);
}
}catch(SQLException e){
System.err.println(e);
}
---------------------------------------------------------
Now you have noticed it from other type databases
with difference in these 2 lines
***************************************************
Line 1:Database Url
-------------
MS access
DriverManager.getConnection("jdbc:odbc:databasename");
Derby database
DriverManager.getConnection("jdbc:derby://localhost:1527/simpledbb,"username","password");
here we used derby
Line 2:Database Driver
-----------
MS ACCESS
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Derby database
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
0 Comments:
Post a Comment