Install mongo DB to Windows
1.Copy & unzip mongoDB folder top of the Desktop.2.Create a folder name "data" parallel to the "bin" folder.
3.Open command prompt in side the "bin" diractory.(Shift+rightclick).
4.Up client server by typing
bin>mongod.exe --dbpath "<path to data folder( get path clicking inside the data folder)>"
eg: bin>mongod.exe --dbpath "C:/sds/mongo/data"
5.While running the server.Create the client.Don't close the server cmd prompt.Just minimize it.
Again go inside the bin folder and open a new command prompt
>mongo.exe
Now client up and running.
>db.help()-Show all commands.
>use <database name> - Create database.
>db.stats()-information about current server.
>db-name of current db.
>show dbs - show all dbs in server.
insert a record
>db.<database name>.insert({"firstname":"Saman","Age":12})
>db.<database name>.find()-find all data.
Without using cmd prompt client using MongoVUE GUI Client.
Run the installer.Just Click Next->Next->FinishClick on the new connection icon.
Name:localhost
Server:localhost
Connect
Create a new DB
Server node(localhost)->Add db->give db name
Create table
Database->add collection->give table name
Insert a record
Collection->Insert/Input Doc
{
"age" :34,
"name":"Saman"
}
Show data
Collection name->Show table(unique id added automatically)
To add another record to existing table,remeber to use same "KEYS"(case sensetive)
CREATE a database name "usermanager"& collection name"user"
fields id(int),firstName(str),lastName(str),email(str).
Create CRUD operations in Java application
File->New->Project->Java->Java Project->Next->Give a name->Finish.
Add mongoDBDriver to the project.
Eclipse
Project->right click->propeties->JavaBuildPath->Libraries->Add External JARS->browse the jar file->OK->close.
Netbeans
Right click on Library->Add JAR/Folder->Select the JAR file->Add
Create a new package.
lk.sd.userhandle.util
1.Add class to database connection
Eclipse
SRC->New->Class->Name the class(DB MANAGER)->FinishNetbeans
Click on the package->Add new class.
|
2. Add another class for "User " entity.
public class User {
private int id;
private String firstName;
private String lastName;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
remove the existing main class and
3.Add new Jframe and name it "Main.java"-Startup page
Call Adduser form
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
AddUser add=new AddUser();
add.setVisible(true);
}
Call UserManager form
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ManageUser mange= new ManageUser();
mange.setVisible(true);
}
Call View user form
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
viewUser view= new viewUser();
view.setVisible(true);
}
Add new jframe
1.Adduser.java
Right click on package->New->Other->Jframe->next->name as "Main"
from swing pallete.
Go to Design->Add a JTabbedpane->then add Jpannel(right click on japannel-Layout->Absolute->
Go propeties->title->Add user.
Add Jalabel,Jtextfields,Jbutton,go to "code" change the nameid property.
1.Double click and Inside the Jbutton(Add user)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
User newUser = new User();
newUser.setId(Integer.parseInt(txtId.getText()));
newUser.setFirstName(txtFirstName.getText());
newUser.setLastName(txtLastName.getText());
newUser.setEmail(txtEmail.getText());
DBObject doc = createDBObject(newUser);
DB userDB = DBManager.getDatabase();
DBCollection col = userDB.getCollection("user");
WriteResult result = col.insert(doc);
JOptionPane.showMessageDialog(rootPane, "User Insert SucessFully...!");
txtId.setText("");
txtFirstName.setText("");
txtLastName.setText("");
txtEmail.setText("");
}
2.Double click and Inside the Jbutton(Exit)
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
this.dispose();
}
3. Next add DB creation method
private static DBObject createDBObject(User user) {
BasicDBObjectBuilder docBuilder = BasicDBObjectBuilder.start();
docBuilder.append("_id", user.getId());
docBuilder.append("firstName", user.getFirstName());
docBuilder.append("lastName", user.getLastName());
docBuilder.append("email", user.getEmail());
return docBuilder.get();
}
2.Add jframe
ManageUser.java/Update/Delete User
Add Jcombo box/Jalabels.Jbuttons
1.Create method to Load the id values to combo box
public void fillID() {
DB userDB = DBManager.getDatabase();
DBCollection col = userDB.getCollection("user");
DBCursor cursor = col.find();
while (cursor.hasNext()) {
DBObject user = (DBObject) cursor.next();
jComboBox1.addItem(user.get("_id"));
}
}
2.Call the method inside constructer
public ManageUser() {
initComponents();
fillID();
}
3.Doble click on the combobox(ComboBox ItemChanged method)
private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
DB userDB = DBManager.getDatabase();
DBCollection col = userDB.getCollection("user");
DBCursor cursor = col.find();
while (cursor.hasNext()) {
DBObject user = (DBObject) cursor.next();
int seletedID = Integer.parseInt(jComboBox1.getSelectedItem().toString());
int id =Integer.parseInt( user.get("_id") .toString() );
if (id == seletedID) {
jTextField1.setText(user.get("_id").toString());
jTextField2.setText(user.get("firstName").toString());
jTextField3.setText(user.get("lastName").toString());
jTextField4.setText(user.get("email").toString());
}
}
}
4.Update selected user
Double click update button.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
DB userDB = DBManager.getDatabase();
DBCollection col = userDB.getCollection("user");
BasicDBObject query = new BasicDBObject("_id", new BasicDBObject("$eq", Integer.parseInt(jTextField1.getText())));
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("firstName", jTextField2.getText());
newDocument.put("lastName", jTextField3.getText());
newDocument.put("email", jTextField4.getText());
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
col.update(query, updateObj, false, true);
JOptionPane.showMessageDialog(rootPane, "User Update SucessFully...!");
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, "Please Select User");
}
}
5.Delete selected user.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
DB userDB = DBManager.getDatabase();
DBCollection col = userDB.getCollection("user");
BasicDBObject query = new BasicDBObject("_id", new BasicDBObject("$eq", Integer.parseInt(jTextField1.getText())));
col.remove(query);
JOptionPane.showMessageDialog(rootPane, "User Delete SucessFully...!");
this.setVisible(false);
this.dispose();
ManageUser mange= new ManageUser();
mange.setVisible(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, "Please Select User");
}
}
6.Add jframe
viewUser.java
Add a jtable
1.Add method to load data from DB
public void printmongData()
{
DefaultTableModel model = new DefaultTableModel();
model.addColumn("ID");
model.addColumn("First Name");
model.addColumn("Last Name");
model.addColumn("Email");
DB userDB = DBManager.getDatabase();
DBCollection col = userDB.getCollection("user");
DBCursor cursor = col.find();
while (cursor.hasNext()) {
DBObject user=(DBObject) cursor.next();
model.addRow(new Object[] {user.get("_id"),user.get("firstName"),user.get("lastName"),user.get("email")});
}
jTable3.setModel(model);
}
2.Call the method inside constructer
public viewUser() {
initComponents();
printmongData();
}




No comments:
Post a Comment