1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need Help: Java > Complain Management System > Database Involved SQL

Discussion in 'Programming' started by GAdsense, Sep 10, 2009.

  1. #1
    I am learning Java language and thought to start with making a software namely Complain Management System. I have limited time, no more than 15-20 hours to submit this project.

    Its a simple one, it handles common day to day computer lab issues. The database is done, the flow is ready, all the forms and GUI is complete.

    What I am stuck at? is inserting the data in the database using SQL. To name them specifically, its "issue complain" and "sign up" process.

    Any and every help would be highly appreciated, thank you.

    For more details, please PM me and we can discuss it via gmail chat, yahoo or even msn.
     
    GAdsense, Sep 10, 2009 IP
  2. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    do you know sql? you will need a java odbc driver that matches your database system (oracle, mysql, mssql, postgresql, etc, etc). once you have this odbc driver you will need to create a connection with the database and voila :) just put the sql commands in your java code.
     
    pneulameiro, Sep 10, 2009 IP
  3. needAdvice

    needAdvice Active Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    As the above user said. download the odbc from MySql site

    Following code can be use if your using MySql Database

    Write a class for connection e.g.


    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.io.*;
    import java.util.*;
    import java.net.*;
    import java.sql.*;


    public class clsConnection {


    public Connection setConnection(Connection conn, String username, String password )

    {
    try
    {

    String url = "jdbc:mysql://127.0.0.1:3306/YOURDATABASE";
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url, username, password);


    }catch(SQLException e)
    {
    System.err.println("SQl Exception");
    e.printStackTrace();

    }
    catch (Exception e)
    {
    System.out.println("\nAnother Error");
    }
    return conn;

    }


    }




    now then declare and call the class in your application as

    clsConnection connect = new clsConnection();

    Connection conn;




    now depend on your requirement you can write a code with SQL. i show you a example for inserting a staff data into login table.


    Say when you click on a button you want to be insert then write a code like

    JButton buttonAddStaff = new JButton("Add Staff");
    buttonAddStaff.addActionListener(this);

    buttonAddStaff.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();

    theUsername = "";
    thePassword = "";
    theStaffType = "";



    theUsername = tfStaffUserName.getText().trim();
    thePassword = tfStaffPassword.getText().trim();
    theStaffType = getSetOpenData.staffType;


    try {
    conn = connect.setConnection(conn,"root","yourpass");
    }
    catch(Exception e)
    {
    }
    try {

    Statement stmt = conn.createStatement();
    if (!theUsername.equals("") && !thePassword.equals("") && !theStaffType.equals("") ) {

    String query = "SELECT * FROM login WHERE username='" + theUsername +"'";
    ResultSet rs = stmt.executeQuery(query);
    int foundrec = 0;
    while (rs.next())
    {
    dialogmessage = "Record Already Exists in DataBase!!!";
    dialogtype = JOptionPane.WARNING_MESSAGE;
    JOptionPane.showMessageDialog((Component)null, dialogmessage, dialogs, dialogtype);

    foundrec = 1;

    }

    if (foundrec == 0){

    String temp = "INSERT INTO login (username,password,access) VALUES ('"+ theUsername +"','"
    + thePassword +"','"
    + theStaffType + "')";

    int result = stmt.executeUpdate( temp );
    if ( result == 1 ){
    System.out.println("Recorded Added");
    ResetRecord();


    }
    else {
    dialogmessage = "Failed To Insert";
    JOptionPane.showMessageDialog(null, "Failed To Insert in DataBase",
    "WARNING!!",JOptionPane.WARNING_MESSAGE);


    }
    }



    }

    else
    {
    dialogmessage = "Empty Record !!!";
    dialogtype = JOptionPane.WARNING_MESSAGE;
    JOptionPane.showMessageDialog((Component)null, dialogmessage, dialogs, dialogtype);

    }

    conn.close();

    }

    catch(Exception ex) {
    JOptionPane.showMessageDialog(null,"GENERAL EXCEPTION", "WARNING!!!",JOptionPane.INFORMATION_MESSAGE);
    }





    hope this helps. good luck!
     
    needAdvice, Sep 10, 2009 IP
  4. GAdsense

    GAdsense Well-Known Member

    Messages:
    1,247
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    140
    #4
    I've just seen this thread again, thanks for the help.

    Well, I know SQL language, I have the driver and I've connected it to retrive data from database as well.

    I am finding issues with inserting data and writing the Java code, if may be we can chat?
     
    GAdsense, Sep 10, 2009 IP
  5. needAdvice

    needAdvice Active Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    Send me the code where you got problem via PM so i have a look.


     
    needAdvice, Sep 10, 2009 IP
  6. GAdsense

    GAdsense Well-Known Member

    Messages:
    1,247
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Sure, please give me a few minutes. Am trying out some thing, see if it works. I will surely get back to you.
     
    GAdsense, Sep 10, 2009 IP
  7. GAdsense

    GAdsense Well-Known Member

    Messages:
    1,247
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Guys, just wanted to share the good news with all of you. We achieved second position for the software competition. So the credit goes to all of you too :) Thanks.

    Trying to enhance Java knowledge, if you like to help with this
    http://forums.digitalpoint.com/showthread.php?t=1507202
     
    GAdsense, Sep 23, 2009 IP
  8. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    May be you can give to me some kind of gift :) What kind of competition are you in?
     
    pneulameiro, Sep 23, 2009 IP
    GAdsense likes this.
  9. GAdsense

    GAdsense Well-Known Member

    Messages:
    1,247
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    140
    #9
    Well it was a basic software competition I took part in, as I liked Java language so I participated. Java was new for me, so don't get so excited.... was just taking a basic course cause of interest :) and as a gift I've already thanked you and added reputation my friend.
     
    GAdsense, Sep 23, 2009 IP