Classe nao inclui da minha tabela do banco de dados

1 resposta
E

entrei criei a classe main
criei minha classe conexao
e a minha classe insert
a conexao esta fazendo certinho
mas a classe insert nao esta inserindo na tabela

/*

  • Main.java
  • Created on 28 de Outubro de 2006, 14:16
  • To change this template, choose Tools | Template Manager
  • and open the template in the editor.
    */
package conexaobanco;

import java.sql.Connection;

import java.sql.SQLException;

/**
*

  • @author Evandro
    */
    public class Main {

    /** Creates a new instance of Main */
    
    public Main() {
    
    }
    

    /**

    • @param args the command line arguments
      
      */
      
      public static void main(String[] args) {
      
      try{
      
      Connection conn = ControladorDeConexoes.abreConexao();
      
      insert pegadados = new insert();
      
      if(conn == null){
               System.out.println("não conseguiu conectar");
      
    }else{
    
    System.out.println(Ok);
    
    conn.close();
    
    }
    
} catch (SQLException e) {

///throw new Exception(“Erro.”);

System.out.println(Deu exceção);

}
}

}

package conexaobanco;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

/**
*

  • @author Evandro
    */
    public class ControladorDeConexoes {

    private static Connection conn;
    
    private static final String URL = jdbc:mysql://localhost:3306/universidade”;
    
    private static final String DRIVER = com.mysql.jdbc.Driver;
    
    private static final String USUARIO = root;
    
    private static final String PASSWORD = “”;
    
    public static Connection abreConexao() throws SQLException {
    
    try {
    
    if (conn == null) {
    
    Class.forName(DRIVER);
    
    conn = DriverManager.getConnection(URL, USUARIO, PASSWORD);
    
    }
    
    return conn;
    
    } catch (ClassNotFoundException e) {
    
    throw new SQLException(e.getMessage());
    
    }
    
    }
    

}

  • insert.java
  • Created on 28 de Outubro de 2006, 15:43
  • To change this template, choose Tools | Template Manager
  • and open the template in the editor.
    */
package conexaobanco;

import java.sql.Connection;

import java.sql.SQLException;

//import java.sql.*;

/**
*

  • @author Evandro
    */
    public class insert {

public void insert()throws SQLException {

Connection conn = null;

///try {

conn = ControladorDeConexoes.abreConexao();
String sql=insert into contato (nome) values (?);

java.sql.PreparedStatement pStmt = conn.prepareStatement(sql);

pStmt.setString(1,teste);

////////introw = pStmt.executeQuery();

pStmt.executeUpdate();

////java.sql.Statement stmt = conn.createStatement();

////int rows = stmt.executeUpdate(“insert into contato (nome) values (teste)”);
//stmt.close();conn.close();

///Statement stmt = conn.createStatement();
///ResultSet rs = stmt.executeQuery(“SELECT a, b, c FROM contatos”);

System.out.println(inserir dados);

//}

///catch (ClassNotFoundException e) {

//            throw new SQLException(e.getMessage());

///}

}

}

o que esta acontecendo??

Evandro

1 Resposta

Marcel_Horner

Faltou o commit no banco, coloque conforme abaixo:

pStmt.executeUpdate(); 
conn.commit();
Criado 2 de novembro de 2006
Ultima resposta 2 de nov. de 2006
Respostas 1
Participantes 2