Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Connecting to a database

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connecting to a database

    I have wrote this small program to connect to a database using a JTDS ODBC driver and it seems to be finding the driver and all but I dont think its connecting to the database can some please tell me if my code looks ok

     
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.sql.*;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
     
     
     
    public class Gui extends JFrame
    {
    	private JComboBox box;
    	private JButton reg;
     
     
     
    	private static String[] filename = {"Sign In", "Sign Out", "Break In", "Break Out", 
    		"Lunch In", "Lunch Out"};
     
     
     
     
    	public Gui ()
    	{
    		super("CAW Time Clock");
    		setLayout(new FlowLayout());
     
    		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:");
     
     
     
    		box = new JComboBox(filename);
    		reg = new JButton("Submit");
     
    		try {	//Connecting the driver
    				Class.forName("net.sourceforge.jtds.jdbc.Driver");
    				Connection conn = DriverManager.getConnection ("jdbc:jtds:sqlserver://DATASERVER1:1433/Time_Log_CSR");
    				//Inserting the statement to the database
    				Statement statement = conn.createStatement();
    				statement.executeUpdate(" INSERT INTO TimeLog ( LogDate, Name, LogReason, TimeInOrOut)" 
    						+ "VALUES (null, washington, null, null)");
     
    		}
    		catch ( Exception e ) 
    		{
    			e.printStackTrace();
    		}
     
     
     
     
    		add(box);
    		add(reg);
     
    		//get current date time with Date()
    		Date date = new Date();
    		System.out.println(dateFormat.format(date));
     
     
     
     
    		HandlerClass handler = new HandlerClass();
    		reg.addActionListener(handler);
     
    	}
     
    	private class HandlerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			JOptionPane.showMessageDialog(null, String.format(" Your Time has been recored", event.getActionCommand()));
    		}
    	}
     
     
     
     
    }


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Connecting to a database

    Are you seeing any exceptions by any chance?

    // Json

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connecting to a database

    No Im not.... its acting like its going to the database but when I go to the database I see not activity

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Connecting to a database

    I have moved this thread to: JDBC & Databases - Java Programming Forums

    Please make sure you post in the most relevant category. As your question is database related, our JDBC & Databases forum is the best.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Connecting to a database

    I cant really see why it wouldnt work just by looking at your code, however you should make sure you close your resources after use, preferably in a finally block.

    // Json

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connecting to a database

    Thanks Json for the help and info

Similar Threads

  1. Replies: 1
    Last Post: December 2nd, 2009, 04:01 AM
  2. java application connecting to a server
    By wildheart25c in forum Java Networking
    Replies: 2
    Last Post: September 17th, 2009, 07:22 AM
  3. Help about connecting DB to java!
    By java_cs in forum JDBC & Databases
    Replies: 1
    Last Post: September 11th, 2009, 12:39 PM
  4. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM
  5. Replies: 1
    Last Post: November 12th, 2008, 05:16 PM