Nao consigui incluir no my sql - Alguem me ajudem (novamente)

  • 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();
      insert.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”);
}

}    

}

controladorde contaxao

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 static void insert()throws SQLException {
///public static insert insert()throws SQLException {

///Connection conn = null;
///try {
//conn = ControladorDeConexoes.abreConexao();
Connection conn = ControladorDeConexoes.abreConexao();

String sql=“insert into contato (nome) values (?)”;
java.sql.PreparedStatement pStmt = conn.prepareStatement(sql);
pStmt.setString(1,“teste”);
pStmt.executeUpdate();
conn.commit();
System.out.println(“inserir dados”);

}

}

Pessoal, nao estou conseguindo fazer a insercao, quando vai executar a linha “pStmt.executeUpdate();” da classe insert, aparece o erro de excecao da classe main

no aguardo

Evandro

evandro_araujo,

Substitua:
pStmt.executeUpdate();

Por:
pStmt.execute();

ASOBrasil

Nao pode ser o executeUpdate()?

Segundo o JavaDoc deveria funcionar:

executeUpdate() Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.

Evandro só pra te dizer uma coisinha, na sua classe main vc faz o seguinte: Connection conn = ControladorDeConexoes.abreConexao();
e depois repara bem vc instancia a classe insert (que por sinal nome minusculo na classe não é convenção), coloque desse jeito:

public class Main {

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

/**

  • @param args the command line arguments
    */
    public static void main(String[] args) {
    try{
    insert pegadados = new insert();
    insert.insert();

} catch (SQLException e) {
System.out.println(“Deu exceção”);
}

}

}


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());
}
}


public class insert {

public void insert()throws SQLException {

Connection conn = ControladorDeConexoes.abreConexao();

String sql=“insert into contato (nome) values (?)”;
java.sql.PreparedStatement pStmt = conn.prepareStatement(sql);
pStmt.setString(1,“teste”);
pStmt.execute();
conn.commit();
conn.close();
System.out.println(“inserir dados”);

}

}