DriverManager似乎不会连接DB

时间:2014-05-24 23:13:59

标签: java mysql

我正在为客户端/服务器应用程序完成学校作业,并且似乎无法使用JDBC DriverManager进行连接。我已经为我正在使用的架构设置了驱动程序,it351,并且似乎按顺序排列了所有内容,但它从不会通过连接触发,告诉我它已连接。人群中任何智慧的话语?我确实在服务任务窗格中显示了驱动程序,并且我保存了密码,因此我不必与该类共享它。感谢。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;

public class GUI extends JFrame
{
   private JButton custButton;
   private JButton prodButton;
   private JButton exitButton;
   private JPanel p1;
   private JPanel p2;
   private JPanel p3;
   private JTextArea data;
  // private String message = ""; // message from server
   private String localhost; // host server for this application

public GUI()
   {
      super( "Database Data Retrieval" );
      setSize(825, 800);
      setVisible(true);
      setLocationRelativeTo(null);
      setResizable(true);
      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      Container c = new Container();
      add(c);
      c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));

      // create and add jpanels for components
      p1 = new JPanel(); //(new FlowLayout(FlowLayout.CENTER));
      c.add(p1);
      p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
      c.add(p2);
      p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
      c.add(p3);

      data = new JTextArea(35, 70);
      JScrollPane dataScroll = new JScrollPane(data);
      dataScroll.setPreferredSize(null);
      p1.add(data);
      p1.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
      data.setBorder(BorderFactory.createLineBorder(Color.black));
      data.setVisible(true);

      // create buttons
      p2.setBorder(BorderFactory.createTitledBorder( null, "Database Controls"));
      custButton = new JButton( "Retrieve Customer Data");
      p2.add (custButton);
      p2.add(Box.createRigidArea(new Dimension(50,0)));
      prodButton = new JButton( "Retrieve Product Data" );
      p2.add(prodButton);
      p2.add(Box.createVerticalGlue());
      p2.add(Box.createHorizontalGlue());
      exitButton = new JButton ("    Exit    ");
      p3.add(exitButton);
      p3.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));

// register events for buttons
ButtonHandler handler = new ButtonHandler();
      custButton.addActionListener( handler );
      prodButton.addActionListener( handler );
      exitButton.addActionListener( handler );
}


   private void displayMessage( final String messageToDisplay )
   {
      SwingUtilities.invokeLater(
         new Runnable()
         {
            public void run() // updates displayArea
            {
               data.append( messageToDisplay );
            } // end method run
         }  // end anonymous inner class
      ); // end call to SwingUtilities.invokeLater
   } // end method displayMessage

   private void connect()
   {
       Connection conn = null;
       try
                {
                Class.forName("com.mysql.jdbc.Driver"); 
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/it351?zeroDateTimeBehavior=convertToNull", "root", ""); //Connect
                    displayMessage("******* Connection created successfully *******"); //informs user of connection 
            }           
             catch (Exception err)
                {
                }
   }

   private class ButtonHandler implements ActionListener 
   {
      @Override
      public void actionPerformed( ActionEvent event )
      {         
         if (event.getSource() == custButton)         
         {   
             displayMessage( "******* Attempting connection *******\n" );
             connect();           
         }


         if (event.getSource() == prodButton)
         {                 

         }



         if (event.getSource() == exitButton)
         {
             System.exit(0);
         }

    }// end of actionperformed
}// end of buttonhandler 

}// end of GUI class

0 个答案:

没有答案