Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By Tolls

Thread: Failed to connect to MySQL database

  1. #1
    eminempark is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Failed to connect to MySQL database

    Currently I am writing a Java program to connect to the database and here is my program:

    Java Code:
    import java.io.*;
    
    import java.awt.*;
    
    import java.awt.event.*;
    
    import javax.swing.*;
    
    import javax.swing.border.*;
    
    import java.util.Timer;
    
    import java.util.TimerTask;
    
    import javax.swing.table.*;
    
    import java.sql.*;
    
    import java.util.Date;
    
    import java.util.Calendar;
    
    import java.util.Vector;
    
    import java.text.SimpleDateFormat;
    
    import java.text.ParseException;
    
    import java.lang.String;
    
    import java.text.DecimalFormat;
    
    
    
    /**
    
    * these 3 are called form the tinyos.jar,
    
    * and need to be placed in where the
    
    * java\jdk<version>\jre\lib\ext located.
    
    */
    
    import net.tinyos.packet.*;
    
    import net.tinyos.util.*;
    
    import net.tinyos.message.*;
    
    
    
    /*
    
     *
    
     * This file is to create tabs, link tabs,
    
     * and create table in database by asking user input the table name
    
     *
    
     */
    
    
    
    public class CAS_Main extends JPanel {
    
    	CAS_Sub1 cas_sub1 = new CAS_Sub1();
    
    	credits credits = new credits();
    
    	JLabel label2;
    
    
    
    	Connection db = null; // Database
    
    	Statement sql = null; // Statement for database
    
    
    
    	static String tablename = new String(); // Used to store table name
    
    
    
    	JTextField textfield;
    
    
    
    	public CAS_Main() {
    
       	super(new BorderLayout());
    
    		JTabbedPane tabbedPane = new JTabbedPane();
    
    
    
    		JPanel tab0 = new JPanel();
    
    		tab0.add(main_panel());
    
    		tab0.add(cas_sub1.sub_connection(1));
    
    		tab0.add(cas_sub1.sub_stream_data(1));
    
    		tabbedPane.addTab("Main Status", tab0);
    
    
    
    		JPanel tab1 = new JPanel();
    
    		tab1.add(credits.credit());
    
    		tab1.add(credits.advisor());
    
    		tab1.add(credits.student());
    
    		tabbedPane.addTab("Credits", tab1);
    
    
    
    		// Connect to database by using jdbc driver
    
    		try {
    
    			Class.forName("com.mysql.jdbc.Driver");
    
    		}
    
    		catch (Exception e) {
    
    	  		System.err.println("Cannot reach the database driver.");
    
    	  		System.exit(-1);
    
    		}
    
    		try {
    
    			db = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "root", "root"); //root, abcd
    
    			sql = db.createStatement();
    
    		}
    
    		catch (Exception e) {
    
    	  		System.err.println("Failed to connect database.");
    
    	  		System.exit(-1);
    
    		}
    
    		add(tabbedPane, BorderLayout.CENTER);
    
    	}
    
    
    
    	/* Creating a table by getting user input */
    
    	protected void CreateTable() {
    
    			try {
    
    				tablename = textfield.getText();
    
    
    
    				sql.executeUpdate("create table " + tablename + "(pc_timestamp datetime, " +
    
    				"node_id int4, packet_id int4, Temp_val float4, Accel_x float4, Accel_y float4, Accel_z float4, Battery float4);");
    
    				System.out.println("Table "+textfield.getText()+" created.");
    
    				CAS_Sub1.textarea.append("Table "+textfield.getText()+" created.\n");
    
    			}
    
    			catch (Exception e) {
    
    			   	System.err.println("Failed to create table.");
    
    			   	CAS_Sub1.textarea.append("Table "+textfield.getText()+" already exist in database.\n");
    
    			   	System.out.println("Table "+textfield.getText()+" already exist in database.");
    
    		   }
    
    	}
    
    
    
    
    
    	/* Get user input for table name */
    
    	protected void SetTable() {
    
    		tablename = textfield.getText();
    
    	}
    
    
    
    	/* Deleting a table by getting user input */
    
    	protected void DeleteTable() {
    
    			try {
    
    				tablename = textfield.getText();
    
    
    
    				sql.executeUpdate("drop table " + tablename);
    
    				System.out.println("Table "+textfield.getText()+" is deleted.");
    
    				CAS_Sub1.textarea.append("Table "+textfield.getText()+" is deleted.\n");
    
    			}
    
    			catch (Exception e) {
    
    			   	System.err.println("Failed to delete table.");
    
    			   	CAS_Sub1.textarea.append("Table "+textfield.getText()+" does not exist in database.\n");
    
    			   	System.out.println("Table "+textfield.getText()+" does not exist in database.");
    
    		   }
    
    	}
    
    
    
    	/* Creating panel */
    
    	protected JPanel main_panel() {
    
    		JPanel pane = new JPanel();
    
    		label2 = new JLabel("");
    
    		pane.setBorder(BorderFactory.createTitledBorder("Create Table"));
    
    		JLabel label = new JLabel("Input table name: ");
    
    		textfield = new JTextField("table1", 30);
    
    
    
    		JButton button3 = new JButton("Delete Table");
    
           button3.setVerticalTextPosition(AbstractButton.BOTTOM);
    
           button3.setHorizontalTextPosition(AbstractButton.CENTER);
    
           button3.addActionListener(new java.awt.event.ActionListener() {
    
    			public void actionPerformed(ActionEvent e) {
    
    				DeleteTable();
    
    			}
    
    	   });
    
    
    
    		JButton button2 = new JButton("Set");
    
         	button2.setVerticalTextPosition(AbstractButton.BOTTOM);
    
          	button2.setHorizontalTextPosition(AbstractButton.CENTER);
    
            button2.addActionListener(new java.awt.event.ActionListener() {
    
    			public void actionPerformed(ActionEvent e) {
    
    				SetTable();
    
    			}
    
    	   });
    
    	   JButton button1 = new JButton("Create Table");
    
           button1.setVerticalTextPosition(AbstractButton.BOTTOM);
    
           button1.setHorizontalTextPosition(AbstractButton.CENTER);
    
           button1.addActionListener(new java.awt.event.ActionListener() {
    
    			public void actionPerformed(ActionEvent e) {
    
    				CreateTable();
    
    			}
    
    	   });
    
    
    
    
    
    	   pane.add(label);
    
    	   pane.add(textfield);
    
    	   pane.add(button2);
    
    	   pane.add(button1);
    
    	   pane.add(button3);
    
    	   return pane;
    
    	}
    
    
    
    }
    And I am successfully compile it. But when I run it, It is failed to connect to database:
    Java Code:
    Failed to connect database.
    Please help me...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Failed to connect to MySQL database

    Instead of using your own message in those catch blocks, use e.printStackTrace().
    That will tell you exactly what went wrong and where.
    DarrylBurke and peyush like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: Failed to connect to MySQL database

    I always have to register my driver in order to get it to work, even though most people leave that part out:

    Java Code:
            DriverManager.RegisterDriver(new com.jdbc.mysql.Driver());  //I do it here
            db = DriverManager.getConnection(url, user, pass);

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Failed to connect to MySQL database

    This goes to the thing I said in the other thread.
    Do not import the driver directly.
    That's the DriverManagers job.

    After all, what if you wanted to swap out drivers?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 05-04-2012, 09:39 AM
  2. How to connect database mysql via Lan
    By ahmed30 in forum JDBC
    Replies: 3
    Last Post: 01-17-2011, 06:36 AM
  3. Replies: 7
    Last Post: 05-09-2010, 12:25 AM
  4. Replies: 15
    Last Post: 04-15-2009, 01:51 AM
  5. Replies: 2
    Last Post: 06-12-2008, 07:09 AM

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
  •