Jdbc - Connect remote Mysql Database error

Posted by Guilherme Ruiz on Stack Overflow See other posts from Stack Overflow or by Guilherme Ruiz
Published on 2012-10-20T04:18:45Z Indexed on 2012/10/20 5:01 UTC
Read the original article Hit count: 526

Filed under:
|
|
|

I'm using JDBC to connect my program to a MySQL database.

I already put the port number and yes, my database have permission to access.

When i use localhost work perfectly, but when i try connect to a remote MySQL database, show this error on console.

java.lang.ExceptionInInitializerError
Caused by: java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at serial.BDArduino.<clinit>(BDArduino.java:25)
Exception in thread "main" Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 1 segundo)

Thank you in Advance !

MAIN CODE

     /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serial;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Ruiz
 */
public class BDArduino extends JFrame {

    static boolean connected = false;
    static int aux_sql8 = Integer.parseInt(Sql.getDBinfo("SELECT * FROM arduinoData WHERE id=1", "pin8"));
    static int aux_sql2 = Integer.parseInt(Sql.getDBinfo("SELECT * FROM arduinoData WHERE id=1", "pin2"));
    CommPort commPort = null;
    SerialPort serialPort = null;
    InputStream inputStream = null;
    static OutputStream outputStream = null;
    String comPortNum = "COM10";
    int baudRate = 9600;
    int[] intArray = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

    /**
     * Creates new form ArduinoTest
     */
    public BDArduino() {
        //super("Arduino Test App");
        initComponents();

    }

    class Escrita extends Thread {

        private int i;

        public void run() {

            while (true) {
                System.out.println("Número :" + i++);
            }

        }
    }

    //public void actionPerformed(ActionEvent e) {
    //    String arg = e.getActionCommand();
    public static void writeData(int a) throws IOException {
        outputStream.write(a);
    }

    public void action(String arg) {
        System.out.println(arg);
        Object[] msg = {"Baud Rate: ", "9600", "COM Port #: ", "COM10"};
        if (arg == "connect") {
            if (connected == false) {
                new BDArduino.ConnectionMaker().start();
            } else {
                closeConnection();
            }
        }

        if (arg == "disconnect") {
            serialPort.close();
            closeConnection();

        }
        if (arg == "p2") {
            System.out.print("Pin #2\n");
            try {
                outputStream.write(intArray[0]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p3") {
            System.out.print("Pin #3\n");
            try {
                outputStream.write(intArray[1]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p4") {
            System.out.print("Pin #4\n");
            try {
                outputStream.write(intArray[2]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p5") {
            System.out.print("Pin #5\n");
            try {
                outputStream.write(intArray[3]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p6") {
            System.out.print("Pin #6\n");
            try {
                outputStream.write(intArray[4]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p7") {
            System.out.print("Pin #7\n");
            try {
                outputStream.write(intArray[5]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p8") {
            System.out.print("Pin #8\n");
            try {
                outputStream.write(intArray[6]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p9") {
            System.out.print("Pin #9\n");
            try {
                outputStream.write(intArray[7]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p10") {
            System.out.print("Pin #10\n");
            try {
                outputStream.write(intArray[8]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p11") {
            System.out.print("Pin #11\n");
            try {
                outputStream.write(intArray[9]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p12") {
            System.out.print("Pin #12\n");
            try {
                outputStream.write(intArray[10]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }
        if (arg == "p13") {
            System.out.print("Pin #12\n");
            try {
                outputStream.write(intArray[11]);
            }//end try
            catch (IOException e12) {
                e12.printStackTrace();
                System.exit(-1);
            }//end catch
        }

    }
    //*******************************************************
//Arduino Connection ***************************************
//******************************************************

    void closeConnection() {
        try {
            outputStream.close();


        } catch (Exception ex) {
            ex.printStackTrace();
            String cantCloseConnectionMessage = "Can't Close Connection!";
            JOptionPane.showMessageDialog(null, cantCloseConnectionMessage, "ERROR", JOptionPane.ERROR_MESSAGE);

        }
        connected = false;
        System.out.print("\nDesconectado\n");

        String disconnectedConnectionMessage = "Desconectado!";
        JOptionPane.showMessageDialog(null, disconnectedConnectionMessage, "Desconectado", JOptionPane.INFORMATION_MESSAGE);

    }//end closeConnection()

    void connect() throws Exception {
        String portName = comPortNum;
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);

        if (portIdentifier.isCurrentlyOwned()) {
            System.out.println("Error: Port is currently in use");
            String portInUseConnectionMessage = "Port is currently in use!\nTry Again Later...";
            JOptionPane.showMessageDialog(null, portInUseConnectionMessage, "ERROR", JOptionPane.ERROR_MESSAGE);

        } else {
            commPort = portIdentifier.open(this.getClass().getName(), 2000);

            if (commPort instanceof SerialPort) {
                serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                outputStream = serialPort.getOutputStream();
            } else {
                System.out.println("Error: Only serial ports are handled ");
                String onlySerialConnectionMessage = "Serial Ports ONLY!";
                JOptionPane.showMessageDialog(null, onlySerialConnectionMessage, "ERROR", JOptionPane.ERROR_MESSAGE);

            }
        }//end else

        //wait some time
        try {
            Thread.sleep(300);
        } catch (InterruptedException ie) {
        }

    }//end connect
    //*******************************************************
//*innerclasses******************************************
//*******************************************************

    public class ConnectionMaker extends Thread {

        public void run() {

            //try to make a connection
            try {
                connect();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.print("ERROR:  Cannot connect!");
                String cantConnectConnectionMessage = "Cannot Connect!\nCheck the connection settings\nand/or your configuration\nand try again!";
                JOptionPane.showMessageDialog(null, cantConnectConnectionMessage, "ERROR", JOptionPane.ERROR_MESSAGE);

            }

            //show status
            serialPort.notifyOnDataAvailable(true);
            connected = true;

            //send ack
            System.out.print("\nConnected\n");
            String connectedConnectionMessage = "Conectado!";
            JOptionPane.showMessageDialog(null, connectedConnectionMessage, "Conectado", JOptionPane.INFORMATION_MESSAGE);

        }//end run
    }//end ConnectionMaker

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        btnp2 = new javax.swing.JButton();
        btncon = new javax.swing.JButton();
        btndesc = new javax.swing.JButton();
        btnp3 = new javax.swing.JButton();
        btnp4 = new javax.swing.JButton();
        btnp5 = new javax.swing.JButton();
        btnp9 = new javax.swing.JButton();
        btnp6 = new javax.swing.JButton();
        btnp7 = new javax.swing.JButton();
        btnp8 = new javax.swing.JButton();
        btn13 = new javax.swing.JButton();
        btnp10 = new javax.swing.JButton();
        btnp11 = new javax.swing.JButton();
        btnp12 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnp2.setText("2");
        btnp2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp2MouseClicked(evt);
            }
        });

        btncon.setText("Conectar");
        btncon.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnconMouseClicked(evt);
            }
        });

        btndesc.setText("Desconectar");
        btndesc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btndescMouseClicked(evt);
            }
        });

        btnp3.setText("3");
        btnp3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp3MouseClicked(evt);
            }
        });

        btnp4.setText("4");
        btnp4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp4MouseClicked(evt);
            }
        });

        btnp5.setText("5");
        btnp5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp5MouseClicked(evt);
            }
        });

        btnp9.setText("9");
        btnp9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp9MouseClicked(evt);
            }
        });

        btnp6.setText("6");
        btnp6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp6MouseClicked(evt);
            }
        });

        btnp7.setText("7");
        btnp7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp7MouseClicked(evt);
            }
        });

        btnp8.setText("8");
        btnp8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp8MouseClicked(evt);
            }
        });

        btn13.setText("13");
        btn13.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn13MouseClicked(evt);
            }
        });

        btnp10.setText("10");
        btnp10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp10MouseClicked(evt);
            }
        });

        btnp11.setText("11");
        btnp11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp11MouseClicked(evt);
            }
        });

        btnp12.setText("12");
        btnp12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnp12MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btncon)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btndesc))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnp6, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnp7, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnp8, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnp9, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnp10, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnp11, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnp12, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn13, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnp2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnp3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnp4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnp5, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btncon)
                    .addComponent(btndesc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnp2)
                    .addComponent(btnp3)
                    .addComponent(btnp4)
                    .addComponent(btnp5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnp6)
                    .addComponent(btnp7)
                    .addComponent(btnp8)
                    .addComponent(btnp9))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnp10)
                    .addComponent(btnp11)
                    .addComponent(btnp12)
                    .addComponent(btn13))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>                        

    private void btnp2MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        action("p2");
    }                                  

    private void btnconMouseClicked(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        action("connect");
    }                                   

    private void btndescMouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        action("disconnect");
    }                                    

    private void btnp3MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        action("p3");
    }                                  

    private void btnp4MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        action("p4");
    }                                  

    private void btnp5MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here
        action("p5");
    }                                  

    private void btnp9MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        action("p9");
    }                                  

    private void btnp6MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        action("p6");
    }                                  

    private void btnp7MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        action("p7");
    }                                  

    private void btnp8MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        action("p8");
    }                                  

    private void btn13MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        action("p13");
    }                                  

    private void btnp10MouseClicked(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        action("p10");
    }                                   

    private void btnp11MouseClicked(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        action("p11");
    }                                   

    private void btnp12MouseClicked(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        action("p12");
    }                                   

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws IOException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
        }

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BDArduino().setVisible(true);
            }
        });
        //}
        while (true) {
            //
            int sql8 = Integer.parseInt(Sql.getDBinfo("SELECT * FROM arduinoData WHERE id=1", "pin8"));
            if (connected == true && sql8 != aux_sql8) {
                aux_sql8 = sql8;
                if(sql8 == 1){
                writeData(2);
                }else{
                writeData(3);
                }
            }
            int sql2 = Integer.parseInt(Sql.getDBinfo("SELECT * FROM arduinoData WHERE id=1", "pin2"));
            if (connected == true && sql2 != aux_sql2) {
                aux_sql2 = sql2;
                if(sql2 == 1){
                writeData(4);
                }else{
                writeData(5);
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();


            }
        }
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton btn13;
    private javax.swing.JButton btncon;
    private javax.swing.JButton btndesc;
    private javax.swing.JButton btnp10;
    private javax.swing.JButton btnp11;
    private javax.swing.JButton btnp12;
    private javax.swing.JButton btnp2;
    private javax.swing.JButton btnp3;
    private javax.swing.JButton btnp4;
    private javax.swing.JButton btnp5;
    private javax.swing.JButton btnp6;
    private javax.swing.JButton btnp7;
    private javax.swing.JButton btnp8;
    private javax.swing.JButton btnp9;
    // End of variables declaration                   
}

© Stack Overflow or respective owner

Related posts about java

Related posts about mysql