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.
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.
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!
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?
Sure, please give me a few minutes. Am trying out some thing, see if it works. I will surely get back to you.
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
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.