Error connecting Java with SQL

0

I have a problem with this code:

CODE

package restaurante2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.SQLException;
//import.java.util.*;

public class Registro extends javax.swing.JFrame {

    public Registro() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    public class Conexion {
        public Connection getConnection() throws Exception {
        String servidor="127.0.0.1:8080";
        String user="system";
        String pass="2100";         
        String driver="oracle.jdb.driver.OracleDiver";
        Class.forName("oracle.jdbc.OracleDriver");
        Connection   conexion=DriverManager.getConnection("127.0.0.1:8080,restaurante,2100");    
        return conexion;
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabelNumOrden = new javax.swing.JLabel();
        jLabelCliente = new javax.swing.JLabel();
        jLabelPlatillo = new javax.swing.JLabel();
        jTextNumOrden = new javax.swing.JTextField();
        jTextFieldCliente = new javax.swing.JTextField();
        jTextFieldPlatillo = new javax.swing.JTextField();
        Alta = new javax.swing.JButton();
        Baja = new javax.swing.JButton();
        Editar = new javax.swing.JButton();
        jButtonSAL = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelNumOrden.setText("NumOrden");

        jLabelCliente.setText("Cliente");

        jLabelPlatillo.setText("Platillo");

        jTextNumOrden.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextNumOrdenKeyTyped(evt);
            }
        });

        jTextFieldCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldClienteActionPerformed(evt);
            }
        });
        jTextFieldCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldClienteKeyTyped(evt);
            }
        });

        Alta.setText("Alta");
        Alta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AltaActionPerformed(evt);
            }
        });

        Baja.setText("Baja");
        Baja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BajaActionPerformed(evt);
            }
        });

        Editar.setText("Editar");
        Editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditarActionPerformed(evt);
            }
        });

        jButtonSAL.setText("X");

        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(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelPlatillo)
                    .addComponent(jLabelCliente)
                    .addComponent(jLabelNumOrden))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextFieldCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                    .addComponent(jTextNumOrden)
                    .addComponent(jTextFieldPlatillo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonSAL))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(58, Short.MAX_VALUE)
                .addComponent(Alta)
                .addGap(44, 44, 44)
                .addComponent(Baja)
                .addGap(52, 52, 52)
                .addComponent(Editar)
                .addGap(81, 81, 81))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabelNumOrden)
                                    .addComponent(jTextNumOrden, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(39, 39, 39)
                                .addComponent(jLabelCliente))
                            .addComponent(jTextFieldCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButtonSAL))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelPlatillo)
                    .addComponent(jTextFieldPlatillo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Alta)
                    .addComponent(Baja)
                    .addComponent(Editar))
                .addGap(53, 53, 53))
        );

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

    private void AltaActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
    }                                    

    private void BajaActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
    }                                    

    private void EditarActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
    }                                      

    private void jTextNumOrdenKeyTyped(java.awt.event.KeyEvent evt) {                                       
     char c= evt.getKeyChar();     

     if (Character.isLetter(c)) {
        getToolkit().beep();

        evt.consume();


     }
    }                                      

    private void jTextFieldClienteActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
    }                                                 

    private void jTextFieldClienteKeyTyped(java.awt.event.KeyEvent evt) {                                           
       //evento envocado
        char c=evt.getKeyChar();
        //convertimos char la tecla pulsada

        if(Character.isDigit(c)) {
            //pregunta si es digito
            getToolkit().beep();
            //alerta
            evt.consume();
            //bloquea
            Error.setText("Solo letras");

        }
    }                                          

    public static void main(String []args) throws Exception {

      String jTextFieldCliente = null;
    String jTextFieldPlatillo = null;
    int jTextNumOrden = 0;

    Connection con = null;
    PreparedStatement pstmt = null;
    String sql = null;  

    try{
        con=DriverManager.getConnection("127.0.0.1:8080,restaurante,2100");
        String query = "insert into menu (numorden,cliente,platillo)values(?,?,?)";
        pstmt = con.prepareStatement(query);
        pstmt.setInt(1, jTextNumOrden);
        pstmt.setString(2,jTextFieldCliente);
        pstmt.setString(3, jTextFieldPlatillo);
        pstmt.executeUpdate();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        pstmt.close();
        con.close();
      }
    }

    public static void Update(){
    String jTextFieldCliente = null;
    String jTextFieldPlatillo = null;
    int jTextNumOrden = 0;

    Connection con = null;
    PreparedStatement pstmt = null;
    String sql = null;
     try{
        con=DriverManager.getConnection("127.0.0.1:8080,restaurante , 2100");
        String query = "delete from menu where (numorden = ?,cliente = ? and platillo =?)";

        pstmt = con.prepareStatement(query);
        pstmt.setInt(1, jTextNumOrden);
        pstmt.setString(2,jTextFieldCliente);
        pstmt.setString(3, jTextFieldPlatillo);
        pstmt.executeUpdate();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        pstmt.close();
        con.close();
      }
    }
    public static void Delete(){
    String jTextFieldCliente = null;
    String jTextFieldPlatillo = null;
    int jTextNumOrden = 0;

    Connection con = null;
    PreparedStatement pstmt = null;
    String sql = null;

    try{
        con=DriverManager.getConnection("127.0.0.1:8080,restaurante , 2100");
        String query = "de into menu (numorden,cliente,platillo)values(?,?,?)";
        pstmt = con.prepareStatement(query);
        pstmt.setInt(1, jTextNumOrden);
        pstmt.setString(2,jTextFieldCliente);
        pstmt.setString(3, jTextFieldPlatillo);
        pstmt.executeUpdate();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        pstmt.close();
        con.close();
      }
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton Alta;
    private javax.swing.JButton Baja;
    private javax.swing.JButton Editar;
    private javax.swing.JButton jButtonSAL;
    private javax.swing.JLabel jLabelCliente;
    private javax.swing.JLabel jLabelNumOrden;
    private javax.swing.JLabel jLabelPlatillo;
    private javax.swing.JTextField jTextFieldCliente;
    private javax.swing.JTextField jTextFieldPlatillo;
    private javax.swing.JTextField jTextNumOrden;
    // End of variables declaration                   
}

Throw the following error

java.sql.SQLException: No suitable driver found for 127.0.0.1:8080,restaurante,2100
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at restaurante2.Registro.main(Registro.java:204)
Exception in thread "main" java.lang.NullPointerException
    at restaurante2.Registro.main(Registro.java:214)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

off topic: I lost access to my previous account, I had a lot of reputation, this I clarify to make it clear that I am not a noob that only seeks to solve a problem, also contributed in its time, this was my user link

    
asked by EduardoCC 25.03.2017 в 05:36
source

2 answers

2

The method getConnection receives by parameter 3 Strings (url,nombreusuario,contraseña) and in its code only sends a single String incorrectly (separated by comma) . also if you use variables for such access it is better to use them. .

I assume that the database is named "restaurant" and that the server runs on port 8080

public Connection getConnection() throws Exception {
    String url ="jdbc:oracle:thin:@127.0.0.1:8080:restaurante";
    String user="system";
    String pass="2100";         
    String driver="oracle.jdb.driver.OracleDiver";
    Class.forName(driver);
    Connection   conexion=DriverManager.getConnection(url,user,pass);    
    return conexion;
}

Keep in mind that the method getConnection (String url) also accepts a single String but you have to build that String correctly.

String URL = "jdbc:oracle:thin:system/[email protected]:8080:restaurante";
Connection conn = DriverManager.getConnection(URL);
    
answered by 25.03.2017 / 06:03
source
1

Your getConnection() for Oracle should look like this:

Connection conn = DriverManager.getConnection
  ("jdbc:oracle:thin:@127.0.0.1:8080:restaurante", "user", "pass");

Where orcl is your BD, user the user and pass the password.

You should also make sure to add the jar of your driver to the Java Build Path of your project

    
answered by 25.03.2017 в 15:00