- 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”);
}
}
}
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);
String sql=“insert into contato (nome) values (?)”;
java.sql.PreparedStatement pStmt = conn.prepareStatement(sql);
pStmt.setString(1,“evandro”);
////////introw = pStmt.executeQuery();
pStmt.executeUpdate();
///pStmt.commit();} return conn; } catch (ClassNotFoundException e) { throw new SQLException(e.getMessage()); }
}
}
Nao consigo inclui na minha tabela, alguem me ajuda??
Evandro