//MySqlTest.java -- Acesso à BD tenis do MySql (servidora titi.lab) via JDBC // Prof. Célio Guimarães 6 Abr 2011 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; public class MySqlTest { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception ex) { // handle the error } Connection conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://titi.lab.ic.unicamp.br/tenis?" + "user=scott&password=tiger"); System.out.println("MySQL Connection OK!!!!\n"); // Do something with the Connection String query= "select numj, nome, pnome from jogadores"; try{ Statement stmt= conn.createStatement(); ResultSet rs= stmt.executeQuery(query); while(rs.next()){ int playernum= rs.getInt("numj"); String nome= rs.getString("nome"); String pnome= rs.getString("pnome"); System.out.println(playernum +"\t" + nome + " \t" + pnome); } } catch (SQLException e){ // handle exception } } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } } }