Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Jdbc

  1. #1
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default Jdbc

    I want to enter a data from the text field to my database named project marking. When I compile the java program seems there's no errors but when I click on the Calculate Button the data won't enter into the database. This is the partial code I didn't finished it yet.


    Java Code:
    import java.awt.event.*;//Package Event Handling
    import javax.swing.*;	//Package Swing
    import java.awt.*;	//Package AWT
    import java.sql.*;	//Package SQL
    //Packages imported
    
    public class PBT extends JFrame 
    //implements ItemListener
    {
    	//Declaration for each components 
    	//Label
    	JLabel regnum= new JLabel("Registration Number: ");
    	JLabel name= new JLabel("Name: ");
    	JLabel peer1= new JLabel("Peer Assessment 1: ");
    	JLabel peer2= new JLabel("Peer Assessment 2: ");
    	JLabel peer3= new JLabel("Peer Assessment 3: ");
    	JLabel totalpeer= new JLabel("Total Peer Assessment: ");
    	JLabel presentation1= new JLabel("Presentation 1: ");
    	JLabel presentation2= new JLabel("Presentation 2: ");
    	JLabel presentation3= new JLabel("Presentation 3: ");
    	JLabel total25= new JLabel("Total Presentation(25%): ");
    	JLabel presentation4= new JLabel("Presentation(15%): ");
    	JLabel capstone= new JLabel("Total Capstone(50%): ");
    	JLabel totalscore= new JLabel("Total Score: ");
    	JLabel disptotalscore= new JLabel("Remove this text after testing");
    	
    	//Button
    	JButton calculateBtn= new JButton("Calculate");
    	JButton resetBtn= new JButton("Reset");
    	
    	//TextField
    	JTextField regnumTf= new JTextField(15);
    	JTextField nameTf= new JTextField(30);
    	JTextField peer1Tf= new JTextField(15);
    	JTextField peer2Tf= new JTextField(15);
    	JTextField peer3Tf= new JTextField(15);
    	JTextField totalpeerTf= new JTextField(15);
    	JTextField presentation1Tf= new JTextField(15);
    	JTextField presentation2Tf=new JTextField(15);
    	JTextField presentation3Tf=new JTextField(15);
    	JTextField total25Tf=new JTextField(15);
    	JTextField presentation4Tf=new JTextField(15);
    	JTextField capstoneTf=new JTextField(15);
    	
    	//Declaring Border Layout
    	BorderLayout border=new BorderLayout();
    	//Declaring Grid Layout
    	GridLayout gridlayout=new GridLayout(0,1);
    	
    	//Creating 4 Panels
    	
    	JPanel pnl2=new JPanel();
    	JPanel pnl3=new JPanel();
    	JPanel pnl4=new JPanel();
    	
    	public PBT()
    	{
    		pnl3.add(regnum);
    		pnl4.add(regnumTf);
    		pnl3.add(name);
    		pnl4.add(nameTf);
    		pnl2.add(calculateBtn);
    		pnl2.add(resetBtn);
    		pnl3.add(peer1);
    		pnl3.add(peer2);
    		pnl3.add(peer3);
    		pnl3.add(totalpeer);
    		pnl3.add(presentation1);
    		pnl3.add(presentation2);
    		pnl3.add(presentation3);
    		pnl3.add(total25);
    		pnl3.add(presentation4);
    		pnl3.add(capstone);
    		//pnl3.add(totalscore);
    		pnl4.add(peer1Tf);
    		pnl4.add(peer2Tf);
    		pnl4.add(peer3Tf);
    		pnl4.add(presentation1Tf);
    		pnl4.add(presentation2Tf);
    		pnl4.add(presentation3Tf);
    		pnl4.add(presentation4Tf);
    		pnl4.add(totalpeerTf);
    		pnl4.add(total25Tf);
    		pnl4.add(capstoneTf);
    		
    		//Add panel to their respective borderlayout
    		add(pnl2,BorderLayout.SOUTH);
    		add(pnl3,BorderLayout.WEST);
    		add(pnl4,BorderLayout.CENTER);
    		
    		//Set the layout
    		pnl3.setLayout(gridlayout);
    		pnl4.setLayout(gridlayout);
    		
    		//Declaring Listener
    		calculateBtn.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    		String regnum1=regnumTf.getText();
    		String name1=nameTf.getText();
    		int p1=Integer.parseInt(peer1Tf.getText());
    		int p2=Integer.parseInt(peer2Tf.getText());
    		int p3=Integer.parseInt(peer3Tf.getText());
    		int pr1=Integer.parseInt(presentation1Tf.getText());
    		int pr2=Integer.parseInt(presentation2Tf.getText());
    		int pr3=Integer.parseInt(presentation3Tf.getText());
    		int pr4=Integer.parseInt(presentation4Tf.getText());
    		int capst=Integer.parseInt(capstoneTf.getText());
    		
    		
    		
    		try{
    		Class.forName("com.mysql.jdbc.Driver");
    		Connection con=DriverManager.getConnection("jdbc:mysql://localhost/projectmarking","root","");
    		Statement st=con.createStatement();
    		
    		int i=st.executeUpdate("insert into student(regnum,name,peer,peer2,peer3,presentation1,presentation2,presentation3,presentation,totalcapstone)values('"+regnum1+"','"+name1+"','"+p1+"','"+p2+"','"+p3+"','"+pr1+"','"+pr2+"','"+pr3+"','"+pr4+"','"+capst+"')");
    		}
    		catch(Exception ex){
    			System.out.println(ex);
    		}
    		
    		}});
    	}
    	
    	public static void main(String[]args)
    	{
    		PBT obj=new PBT();
    		obj.setTitle("PROJECT MARKING");
    		obj.setVisible(true);
    		obj.pack();
    	}
    	
    	
    }
    I get this message when I run the program

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For inpu
    t string: ""
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at PBT$1.actionPerformed(PBT.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Sour
    ce)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Jdbc

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at PBT$1.actionPerformed(PBT.java:103)
    The program tried to convert an empty String to an int on line 103. The program needs to get and test the data before trying to convert it to an int value using the parseInt() method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: Jdbc

    Worth to note: Java is a professional programming language, not a scripting language such as PHP where you are held and nursed in a warm blanket; and in that loving care many assumptions are made for you such as translating an empty string to a literal zero because "usually that is okay". Java is for real applications with real business problems that might involve for example money transactions that run into the billions; it requires incredible precision, which requires you to be incredibly precise too. There are no assumptions, you have to program everything meticulously, precisely and with proper thought behind it.

    That might answer a pending "Why do I have to do this!?" question.
    Norm likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2013, 05:26 PM
  2. Replies: 5
    Last Post: 09-25-2012, 05:36 PM
  3. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Advanced Java
    Replies: 5
    Last Post: 11-22-2011, 04:54 PM
  4. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •