Jump to content

Java Querying SQL Help/Questions

notaniel101
Go to solution Solved by RaidenLeeChin,
Try:
 
public void Querrying(){try {       Class.forName("com.mysql.jdbc.Driver");       Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/linuslinus", "notaniel101", "aaaaa");       Statement st = con.createStatement();                            ResultSet result = st.executeQuery("SELECT * FROM inventory WHERE id = '" + product + "'");      if (!result.isBeforeFirst()) {System.out.println("SORRY THAT PRODUCT ID DOES NOT EXIST");}else{  while (result.next())   {       System.out.println(result.getInt("id"));     System.out.println(result.getInt("quantity"));     System.out.println(result.getString("type"));     System.out.println(result.getString("size"));     System.out.println(result.getString("style"));       ObTYPE = result.getString("type");     ObSIZE = result.getString("size");     ObSTYLE = result.getString("style");     ObQUANTITY = result.getInt("quantity");     NTQUANTITY = ObQUANTITY;     EndingQUANTITY.setText("New Total Quantity ="+ObQUANTITY);     SIZE.setText("Size = "+ObSIZE);     STYLE.setText("Style = "+ObSTYLE);    GENDER.setText("Gender = "+ObTYPE);  }       } result.close(); con.close();   }catch (ClassNotFoundException e) {       System.out.println("Driver not found");}   catch (SQLException e) {       e.printStackTrace();       System.out.println("following error occured "+e);} }

I am working on an inventory tracking app for a local charity as part of my senior computer project. I have it to where it "works" quite well, it can search a sql database and update the database with the appropriate change in quantity that you enter. The part that I am currently having trouble with is that if you enter a product id that doesn't exist it will return all blank values for the respective fields. What I want to do is it to have some sort of message if the id does not exist. Does anybody have any ideas on how to do that (more so the part about making sure the id actually exists) ?

Main rig: Coursair Air 540, Amd 8120(stock clock, had it up to 4.6), Gigabyte 990 fx ud3,  MSI DCU2 r9 280x, seasonic 1000w platinum, Samsung 830 64 GB ssd, Seagate 1tb, 4 GB (2x2) G.Skill, 

 

Secondary: Define r4, Amd A10 5800k(4.6), 1x Sapphire 7950, MSI FM2-A85xa, 8 GB coursair vengeance ram, Seasonic 1250w Gold, 500GB 840 Evo, 223GB 2.5 Hdd, 80 Gb Seasonic, Samsung CD/dvd

Link to comment
Share on other sites

Link to post
Share on other sites

 I briefly tried that and it didn't work, of course I think I also didn't have the code quite right either, but I'll try that again

Main rig: Coursair Air 540, Amd 8120(stock clock, had it up to 4.6), Gigabyte 990 fx ud3,  MSI DCU2 r9 280x, seasonic 1000w platinum, Samsung 830 64 GB ssd, Seagate 1tb, 4 GB (2x2) G.Skill, 

 

Secondary: Define r4, Amd A10 5800k(4.6), 1x Sapphire 7950, MSI FM2-A85xa, 8 GB coursair vengeance ram, Seasonic 1250w Gold, 500GB 840 Evo, 223GB 2.5 Hdd, 80 Gb Seasonic, Samsung CD/dvd

Link to comment
Share on other sites

Link to post
Share on other sites

 I briefly tried that and it didn't work, of course I think I also didn't have the code quite right either, but I'll try that again

Post the code

 

 

You have to check the results in your data models within your application. It makes no sense at all to expect the SQL server to return something other than a empty data set when you query it for data that doesnt exist.

Link to comment
Share on other sites

Link to post
Share on other sites

This is just the method that searches for the information, but it should suffice since its also where I update the Text info boxes

 

public void Querrying(){

try {

       Class.forName("com.mysql.jdbc.Driver");

       Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/linuslinus", "notaniel101", "aaaaa");

       Statement st = con.createStatement();

       

       

       

       ResultSet result = st.executeQuery("SELECT * FROM inventory WHERE id = '" + product + "'");

      

       

       while (result.next()) {

           

        System.out.println(result.getInt("id"));

        System.out.println(result.getInt("quantity"));

        System.out.println(result.getString("type"));

        System.out.println(result.getString("size"));

        System.out.println(result.getString("style"));

       

        ObTYPE = result.getString("type");

        ObSIZE = result.getString("size");

        ObSTYLE = result.getString("style");

        ObQUANTITY = result.getInt("quantity");

        NTQUANTITY = ObQUANTITY;

    EndingQUANTITY.setText("New Total Quantity ="+ObQUANTITY);

    SIZE.setText("Size = "+ObSIZE);

    STYLE.setText("Style = "+ObSTYLE);

    GENDER.setText("Gender = "+ObTYPE);

   

      

       }

       result.close();

       con.close();

     

       

   }

catch (ClassNotFoundException e) {

       System.out.println("Driver not found");

   }

   catch (SQLException e) {

       e.printStackTrace();

       System.out.println("following error occured "+e);

}

 

}

Main rig: Coursair Air 540, Amd 8120(stock clock, had it up to 4.6), Gigabyte 990 fx ud3,  MSI DCU2 r9 280x, seasonic 1000w platinum, Samsung 830 64 GB ssd, Seagate 1tb, 4 GB (2x2) G.Skill, 

 

Secondary: Define r4, Amd A10 5800k(4.6), 1x Sapphire 7950, MSI FM2-A85xa, 8 GB coursair vengeance ram, Seasonic 1250w Gold, 500GB 840 Evo, 223GB 2.5 Hdd, 80 Gb Seasonic, Samsung CD/dvd

Link to comment
Share on other sites

Link to post
Share on other sites

Post the code

 

 

You have to check the results in your data models within your application. It makes no sense at all to expect the SQL server to return something other than a empty data set when you query it for data that doesnt exist.

 

Sorry for double post, didn't remember to quote before I added the code

 

EDIT**

 

The code does not return blank values, it simply does not return anything, which makes sence

Main rig: Coursair Air 540, Amd 8120(stock clock, had it up to 4.6), Gigabyte 990 fx ud3,  MSI DCU2 r9 280x, seasonic 1000w platinum, Samsung 830 64 GB ssd, Seagate 1tb, 4 GB (2x2) G.Skill, 

 

Secondary: Define r4, Amd A10 5800k(4.6), 1x Sapphire 7950, MSI FM2-A85xa, 8 GB coursair vengeance ram, Seasonic 1250w Gold, 500GB 840 Evo, 223GB 2.5 Hdd, 80 Gb Seasonic, Samsung CD/dvd

Link to comment
Share on other sites

Link to post
Share on other sites

Try:
 
public void Querrying(){try {       Class.forName("com.mysql.jdbc.Driver");       Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/linuslinus", "notaniel101", "aaaaa");       Statement st = con.createStatement();                            ResultSet result = st.executeQuery("SELECT * FROM inventory WHERE id = '" + product + "'");      if (!result.isBeforeFirst()) {System.out.println("SORRY THAT PRODUCT ID DOES NOT EXIST");}else{  while (result.next())   {       System.out.println(result.getInt("id"));     System.out.println(result.getInt("quantity"));     System.out.println(result.getString("type"));     System.out.println(result.getString("size"));     System.out.println(result.getString("style"));       ObTYPE = result.getString("type");     ObSIZE = result.getString("size");     ObSTYLE = result.getString("style");     ObQUANTITY = result.getInt("quantity");     NTQUANTITY = ObQUANTITY;     EndingQUANTITY.setText("New Total Quantity ="+ObQUANTITY);     SIZE.setText("Size = "+ObSIZE);     STYLE.setText("Style = "+ObSTYLE);    GENDER.setText("Gender = "+ObTYPE);  }       } result.close(); con.close();   }catch (ClassNotFoundException e) {       System.out.println("Driver not found");}   catch (SQLException e) {       e.printStackTrace();       System.out.println("following error occured "+e);} }

------------------------------------

     ~ Live Love Code ~

------------------------------------

Link to comment
Share on other sites

Link to post
Share on other sites

 

Try:

 

code

Thanks, that works perfectly

Main rig: Coursair Air 540, Amd 8120(stock clock, had it up to 4.6), Gigabyte 990 fx ud3,  MSI DCU2 r9 280x, seasonic 1000w platinum, Samsung 830 64 GB ssd, Seagate 1tb, 4 GB (2x2) G.Skill, 

 

Secondary: Define r4, Amd A10 5800k(4.6), 1x Sapphire 7950, MSI FM2-A85xa, 8 GB coursair vengeance ram, Seasonic 1250w Gold, 500GB 840 Evo, 223GB 2.5 Hdd, 80 Gb Seasonic, Samsung CD/dvd

Link to comment
Share on other sites

Link to post
Share on other sites

Np, btw just double check what happens when data is returned, just to make sure it enters the else.

Don't forget to mark answers as answered :P

------------------------------------

     ~ Live Love Code ~

------------------------------------

Link to comment
Share on other sites

Link to post
Share on other sites

Np, btw just double check what happens when data is returned, just to make sure it enters the else.

Don't forget to mark answers as answered :P

I tested it and it works.

 

On a slightly unrelated note do you have any advice on making a popup that will appear and then disappear after a set time?

Main rig: Coursair Air 540, Amd 8120(stock clock, had it up to 4.6), Gigabyte 990 fx ud3,  MSI DCU2 r9 280x, seasonic 1000w platinum, Samsung 830 64 GB ssd, Seagate 1tb, 4 GB (2x2) G.Skill, 

 

Secondary: Define r4, Amd A10 5800k(4.6), 1x Sapphire 7950, MSI FM2-A85xa, 8 GB coursair vengeance ram, Seasonic 1250w Gold, 500GB 840 Evo, 223GB 2.5 Hdd, 80 Gb Seasonic, Samsung CD/dvd

Link to comment
Share on other sites

Link to post
Share on other sites

You can create "pop ups" as in a window pop up using Java awt and Javax Swing api.

I wrote a JDBC chatbot recently.

Let me clean up the code and paste it here.
It should be easy from there to add extra things like a timer.

------------------------------------

     ~ Live Love Code ~

------------------------------------

Link to comment
Share on other sites

Link to post
Share on other sites

You can create "pop ups" as in a window pop up using Java awt and Javax Swing api.

I wrote a JDBC chatbot recently.

Let me clean up the code and paste it here.

It should be easy from there to add extra things like a timer.

thanks, I have it now where It pops up to tell you that it is not a valid product, but I want a different one to go away after a couple seconds

Main rig: Coursair Air 540, Amd 8120(stock clock, had it up to 4.6), Gigabyte 990 fx ud3,  MSI DCU2 r9 280x, seasonic 1000w platinum, Samsung 830 64 GB ssd, Seagate 1tb, 4 GB (2x2) G.Skill, 

 

Secondary: Define r4, Amd A10 5800k(4.6), 1x Sapphire 7950, MSI FM2-A85xa, 8 GB coursair vengeance ram, Seasonic 1250w Gold, 500GB 840 Evo, 223GB 2.5 Hdd, 80 Gb Seasonic, Samsung CD/dvd

Link to comment
Share on other sites

Link to post
Share on other sites

Ah ok i was going to paste code to just do a simple pop up.

Don't really have time to do a timer cause I'm running on 28hrs no sleep.

But google is your friend lol

 

Simple pop up:

- MAIN

import java.sql.*; public class MysqlConnect {     public static void main(String args[]) throws Exception {        //Class.forName("com.mysql.jdbc.Driver");        //Connection conn = DriverManager.getConnection("jdbc:mysql://","blah", "");                BasicGUI c = new BasicGUI();    }}

- CLASS

 import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import javax.swing.JLabel;import javax.swing.*; public class BasicGUI extends JFrame{        private JLabel tredy=new JLabel();        public BasicGUI(){     init();    }     private void init() {        super.setSize(550,420);        super.setTitle("Test Timer");        super.setLayout(null);        super.getContentPane().setBackground(new Color(84, 255, 159));                //tredy JLabel        tredy.setBounds(200, 265, 160, 25);        this.getContentPane().add(tredy);        tredy.setText("notaniel101 this is a test!");                super.setResizable(false);//restrict the user from resizing the window        super.setVisible(true);    }    }

------------------------------------

     ~ Live Love Code ~

------------------------------------

Link to comment
Share on other sites

Link to post
Share on other sites

 

Ah ok i was going to paste code to just do a simple pop up.

Don't really have time to do a timer cause I'm running on 28hrs no sleep.

But google is your friend lol


Thanks, that helped alot

Main rig: Coursair Air 540, Amd 8120(stock clock, had it up to 4.6), Gigabyte 990 fx ud3,  MSI DCU2 r9 280x, seasonic 1000w platinum, Samsung 830 64 GB ssd, Seagate 1tb, 4 GB (2x2) G.Skill, 

 

Secondary: Define r4, Amd A10 5800k(4.6), 1x Sapphire 7950, MSI FM2-A85xa, 8 GB coursair vengeance ram, Seasonic 1250w Gold, 500GB 840 Evo, 223GB 2.5 Hdd, 80 Gb Seasonic, Samsung CD/dvd

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×