Erro de Banco, eu acho

7 respostas
D

Pessoal, me ajudem, naum sei o que fazer, com esse erro abaixo.

O que pode ser o erro.

java.sql.SQLException: Syntax error or access violation message from server: Yo

u have an error in your SQL syntax.  Check the manual that corresponds to your M

ySQL server version for the right syntax to use near null at line 1at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2234)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1412)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1494)

at com.mysql.jdbc.Connection.execSQL(Connection.java:1905)

at com.mysql.jdbc.Connection.execSQL(Connection.java:1847)

at com.mysql.jdbc.Statement.executeQuery(Statement.java:1182)

at Dinamico.getJContentPane(Dinamico.java:330)

at Dinamico.initialize(Dinamico.java:305)

at Dinamico.(Dinamico.java:85)

at Dinamico.main(Dinamico.java:292)

java.sql.SQLException: Syntax error or access violation message from server: Yo

u have an error in your SQL syntax.  Check the manual that corresponds to your M

ySQL server version for the right syntax to use near null at line 1

Fico no aguardo de uma resposta

7 Respostas

1112

Revise a sua query. Ao que parece, algum argumento está com “null”.

Shelson

passa a query pra gente olhar

D

Bom, pessoal, eu sei que atrapalha quando a mensagem é muito grande, mas resolvi postar o codigo inteiro, pq naum sei ao certto, onde que tá o problema, obrigado pela ajuda.

import java.awt.GraphicsConfiguration;

import java.awt.HeadlessException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Vector;
import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JScrollBar;

import javax.swing.JTextField;

import javax.swing.JButton;

import javax.swing.JTable;

import javax.swing.JScrollPane;

import javax.swing.border.TitledBorder;

import javax.swing.table.DefaultTableModel;

import javax.swing.JPanel;
import com.mysql.jdbc.PreparedStatement;

import java.awt.BorderLayout;

import javax.swing.BoxLayout;

import java.awt.GridBagLayout;

/*
  • Created on Dec 22, 2004
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
    */

/**

  • @author lsantos

  • TODO To change the template for this generated type comment go to

  • Window - Preferences - Java - Code Style - Code Templates
    */
    public class Dinamico extends JFrame {

    static javax.swing.JPanel jContentPane = null;
    
    static ResultSet rs = null;
    
    static Vector v = new Vector();
    
    static JTextField jTextField = null;
    
    public static String sql_str = “”;
    
    public static JPanel jPanel2 = null;
    
    public static Dinamico d = null;
    

    private static final int Id = 0;
    private static Statement st = null;

    private JLabel jLabel = null;
    
    private JButton jButton = null;
    
    private JTable jTable = null;
    
    private JButton jButton4 = null;
    
    private JTable jTable1 = null;
    
    private PreparedStatement pstmt = null;
    
    private String nomeTabela = null;
    
    private JScrollPane jScrollPane = null;
    
    private JPanel jPanel1 = null;
    
    private boolean controle = false;
    
    public String str_Buscar;
    
    public Vector cabecalhos = new Vector();
    
    public Vector registros  = new Vector();
    
    public static Vector elemento = new Vector();
    
    public int contador = 1;
    
    public int contaColuna = 1;
    
    public int cont = 1;
    
    public int posXText = 90;;
    
    public int posXLabel = 10;
    
    private JButton jButton1 = null;
    
    JScrollPane scroller = null;
    
    DefaultTableModel modelo = null;
    
    public String nome = “”;
    
    //comentário sobre a classe Vector
    
    /**
    
    • @throws java.awt.HeadlessException
      */
      public Dinamico() throws HeadlessException {
      super();
      // TODO Auto-generated constructor stub
      initialize();
      }

    /**

    • @param gc
      */
      public Dinamico(GraphicsConfiguration gc) {
      super(gc);
      // TODO Auto-generated constructor stub
      initialize();
      }

    /**

    • @param title
    • @throws java.awt.HeadlessException
      */
      public Dinamico(String title) throws HeadlessException {
      super(title);
      nomeTabela = title;
      // TODO Auto-generated constructor stub
      initialize();
      }

    /**

    • @param title
    • @param gc
      */
      public Dinamico(String title, GraphicsConfiguration gc) {
      super(title, gc);
      // TODO Auto-generated constructor stub
      initialize();
      }
    JLabel getJLabel(String nomeComp, String nomeColuna, int posicao) {
    
    jLabel = new JLabel();
    
    jLabel.setName(jLabel);
    
    if(contador>=9){
    
    contador = 1;
    
    cont++;
    
    if(cont > 4){
    
    System.out.println(teste);
    
    contaColuna = cont*210;
    
    jPanel2.setPreferredSize(new java.awt.Dimension(contaColuna,260));
    
    }
    
    posXLabel = posXLabel + 210;
    
    posXText = posXText +210;
    
    }
     
     jLabel.setBounds(posXLabel, contador*30, 70, 20);
     jLabel.setText(nomeColuna);
    
     return jLabel;
    

    }
    /**

    • This method initializes jTextField

    • @return javax.swing.JTextField	
      
      <em>/
      
      private JTextField getJTextField(String nomeComp, int posicao) {
      
      jTextField = new JTextField();
      
      jTextField.setName(nomeComp);
      
      jTextField.setBounds(posXText, contador</em>30, 100, 20);
      
      contador++;
      
      return jTextField;
      
      }
      

    /**

    • This method initializes jTable

    • @return javax.swing.JTable	
      
      <em>/
      
      private JTable getJTable() {
      
      if (jTable == null) {
      
      jTable = new JTable();
      
      jTable.setModel(new DefaultTableModel());
      
      jTable.setName(“tabela”);
      
      jTable.setShowGrid(false);
      
      jTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
      
      jTable.setPreferredScrollableViewportSize(new java.awt.Dimension(800,900));
      
      jTable.setRowHeight(80);
      
      jTable.setSize(20, 20);
      
      }
      
      return jTable;
      
      }
      
      /</em>*
      
    • This method initializes jScrollPane

    • @return javax.swing.JScrollPane	
      
      <em>/
      
      private JButton getJButton() {
      
      if (jButton == null) {
      
      jButton = new JButton();
      
      jButton.setText(Inserir);
      
      jButton.setBounds(431, 7, 71, 26);
      
      jButton.setName(nbcvhgdc);
      
      jButton.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent e) {
      
      Inserir.inserirRegistro(nomeTabela);
      
      }
      
      });
      
      }
      
      return jButton;
      
      }
      
      /</em>*
      
    • This method initializes jButton4

    • @return javax.swing.JButton	
      
      */
      
      private JButton getJButton4() {
      
      if (jButton4 == null) {
      
      jButton4 = new JButton();
      
      jButton4.setText(Mostrar Tabela);
      
      jButton4.setName(as);
      
      jButton4.setSize(120, 26);
      
      jButton4.setLocation(613, 7);
      
      jButton4.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent e) {
      
      try {
      
      ExibeTabela(rs);
      
      } catch (SQLException e1) {
      
      // TODO Auto-generated catch block
      
      e1.printStackTrace();
      
      }
      
      }
       });
      
      }
      
      return jButton4;
      
      }
      
      /**
      
    • This method initializes jScrollPane

    • @return javax.swing.JScrollPane	
      
      <em>/
      
      private JScrollPane getJScrollPane() {
      
      if (jScrollPane == null) {
      
      jScrollPane = new JScrollPane();
      
      jScrollPane.setBounds(2, 35, 951, 286);
      
      jScrollPane.setViewportView(getJPanel1());
      
      jScrollPane.setName(“scroll”);
      
      }
      
      return jScrollPane;
      
      }
      
      /</em>*
      
    • This method initializes jPanel1

    • @return javax.swing.JPanel	
      
      <em>/
      
      private JPanel getJPanel1() {
      
      if (jPanel1 == null) {
      
      jPanel1 = new JPanel();
      
      jPanel1.setName(“jPanel1”);
      
      jPanel1.add(getJPanel2(), null);
      
      }
      
      return jPanel1;
      
      }
      
      /</em>*
      
    • This method initializes jPanel2

    • @return javax.swing.JPanel	
      
      <em>/
      
      private JPanel getJPanel2() {
      
      if (jPanel2 == null) {
      
      jPanel2 = new JPanel();
      
      jPanel2.setLayout(null);
      
      jPanel2.setPreferredSize(new java.awt.Dimension(951,260));
      
      jPanel2.setName(“jPanel2”);
      
      }
      
      return jPanel2;
      
      }
      
      /</em>*
      
    • This method initializes jButton1

    • @return javax.swing.JButton	
      
      <em>/
      
      private JButton getJButton1() {
      
      if (jButton1 == null) {
      
      jButton1 = new JButton();
      
      jButton1.setLocation(509, 7);
      
      jButton1.setSize(97, 26);
      
      jButton1.setText(Excluir);
      
      jButton1.setName(excluir);
      
      jButton1.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent e) {
      
      System.out.println(jTable.getSelectedColumn());
      
      System.out.println(jTable.getSelectedRow());
      
      elemento = (Vector) registros.get(jTable.getSelectedRow());
      
      System.out.println(elemento.firstElement());
      
      Excluir.excluirRegistro(nomeTabela);
      
      }
      
      });
      
      }
      
      return jButton1;
      
      }
      
      /</em>*
      
    • This method initializes jTable1

    • @return javax.swing.JTable
      */

    public static void main(String[] args) {
    new Dinamico().Principal();

    }
    /**

    • This method initializes this

    • @return void
      */
      private void initialize() {

      this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
      
      this.setTitle(Banco de Dados - Dinâmico);
      
      this.setSize(961, 466);
      
      this.setContentPane(getJContentPane());
      
      this.setVisible(true);
      
      this.setResizable(false);
      
      this.setLocationRelativeTo(null);
      
      d = this;
      
      }
      
      /**
      
    • This method initializes jContentPane

    • @return javax.swing.JPanel
      
      <em>/
      
      private javax.swing.JPanel getJContentPane() {
      
      if(jContentPane == null) {
      
      jContentPane = new javax.swing.JPanel();
      
      jContentPane.setLayout(null);
      
      jContentPane.setName(“tabela”);
      
      jContentPane.add(getJButton(), null);
      
      jContentPane.add(getJScrollPane(), null);
      
      jContentPane.add(getJButton4(), null);
      
      jContentPane.add(getJButton1(), null);
      
      Statement sq_stmt;
      
      if (Conexao.ConexaoRetorno()){
      
      try {
      
      sq_stmt = Conexao.conn.createStatement();
      
      String sql_str = "SELECT * FROM "+nomeTabela;
      
      ResultSet rs = sq_stmt.executeQuery(sql_str);
      
      ResultSetMetaData rsmd = rs.getMetaData();
      
      int ContaColuna = rsmd.getColumnCount();
      
      for(int i = 1; i <= ContaColuna  ; i++ ){
      
      jPanel2.add(getJLabel(“label”+rsmd.getColumnName(i),rsmd.getColumnName(i),i</em>30), null);
      
      jPanel2.add(getJTextField(“text”+rsmd.getColumnName(i),i*30), null);
      
      v.addElement(“text”+rsmd.getColumnName(i));
      
      }
      
      } catch (SQLException e) {
      
      // TODO Auto-generated catch block
      
      e.printStackTrace();
      
      }
      
      }
      

      }
      return jContentPane;

    }

    public void Principal(){
    
    Statement sq_stmt ;
    
    if (Conexao.ConexaoRetorno()){
    
    try {
    
    sq_stmt = Conexao.conn.createStatement(
    
    java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
    
    java.sql.ResultSet.CONCUR_READ_ONLY);
    
    String sql_str = "SELECT * FROM "+nomeTabela;
    
    ResultSet rs = sq_stmt.executeQuery(sql_str);
    
    rs.first();
    
    Dinamico.rs = rs;//acrescentei essa linha caso use o método exibe registro não precisa dela.
    
    }catch(Exception erro){
    
    System.out.println(erro);
    
    }				
    
    }
    
    }
    
    private JTable ExibeTabela(ResultSet rs) throws SQLException{
    
    if(controle == false){
    
    if (!rs.first()) {// Se não houver registros, exibe uma mensagem e termina a execuçao do método
    
    JOptionPane.showMessageDialog(this,O ResultSet está vazio.);
    
    return null;
    
    }
    
    try{
          	// obtém títulos de coluna
             ResultSetMetaData rsmd = rs.getMetaData();
             for (int i = 1; i <= rsmd.getColumnCount(); i++){
             	cabecalhos.addElement(rsmd.getColumnName(i));
             }
             // obtém dados da linha
            
             do{
             	registros.addElement(ProximoRegistro(rs,rsmd));
             }while(rs.next());
             	        
             
             
          // jPanel.setBorder(new TitledBorder("TableDemo.autoresize_mode")); 
                   
             modelo = new DefaultTableModel(registros, cabecalhos); 
             jTable = new JTable(modelo);
             jTable.setModel(modelo);
             jContentPane.add(jTable);
             scroller = new JScrollPane(jTable);
             getContentPane().add(scroller);
             jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
             jContentPane.add(scroller);
             scroller.setBounds(03, 320, 951, 100);
             controle = true;
             jButton4.setText("Fechar Tabela");
             // exibe a tabela com conteúdos de ResultSet
             return jTable;
             
          }catch (SQLException erro){
          	System.out.println(erro);
             return null; 
          }
     }
     else{
     	jContentPane.remove(scroller);
     	cabecalhos.removeAllElements();
     	registros.removeAllElements();
     	jButton4.setText("Mostrar Tabela");
     	jContentPane.repaint();
     	//jTable.setModel(new DefaultTableModel());
     	controle = false;
     	
     }
     return null;
    

    }

    private Vector ProximoRegistro(ResultSet rs, ResultSetMetaData rsmd)throws SQLException{
    
    Vector registro = new Vector();
    
    for ( int i = 1; i <= rsmd.getColumnCount();i++){
    
    registro.addElement(rs.getString(i));
    
    }
    
    return registro;
    
    }
    

} // @jve:decl-index=0:visual-constraint=“10,10”

1112

Quando for postar código, coloque-o entre as seguintes tags:

&#91code] &#91/code]

Assim a identação será mantida e o código ficará mais legível.

O que acontece é o seguinte: sua query lê faz um select a partir da String nomeTabela, mas você não está inicializando esta String. O único lugar onde ela é inicializada é no seguinte construtor:

/**
   * @param title
   * @throws java.awt.HeadlessException
   */
   public Dinamico(String title) throws HeadlessException {
      super(title);
      nomeTabela = title;
      // TODO Auto-generated constructor stub
      initialize();
   }

Mas o construtor que você usa não é esse:

/**
   * This method initializes jTable1
   *
   * @return javax.swing.JTable
   */

   public static void main(String[] args) 
      new Dinamico().Principal();

   }

Você tem que obter o nome da tabela que quer consultar de alguma maneira, para então fazer a query.

D

kra desculpa a minha santa ingnorância, mas eu to começando no java, e naum entendi em nda o que vc disse, tem uma forma mais simples de ser fazer isso???
E valeu pela ajuda, valeu mesmo

1112

Perdão pela demora, mas eu não tinha mais olhado este tópico. :oops:

Vamos supor que você queira fazer a sua query a partir da tabela Teste. Para fazer essa query, você deve indicar o nome da tabela em algum lugar. O único lugar onde esse nome é indicado é no construtor que eu indiquei. Portanto, você deve utilizar aquele editor. Ou seja, no lugar do código a seguir:

/**
   * This method initializes jTable1
   *
   * @return javax.swing.JTable
   */

   public static void main(String[] args) 
      new Dinamico().Principal();

   }

você deveria usar:

/**
   * This method initializes jTable1
   *
   * @return javax.swing.JTable
   */

   public static void main(String[] args) 
      new Dinamico("Teste").Principal();

   }
duardor

E eu pensei que meu nick era unico…
:roll: :roll: :roll: :roll:

Criado 3 de fevereiro de 2005
Ultima resposta 9 de fev. de 2005
Respostas 7
Participantes 4