- 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