nExemplo 1 - JDBC
n
nListagem 01. JDBCCode.java
npackage
jdbc;
nimport
java.sql.*;
npublic
class JDBCCode {
n
private static Connection con = null;
n public
JDBCCode() { }
n public
static Connection open(){
n String
user = "root";
n String
pass = "123456";
n String url = "jdbc:mysql://localhost:3306/BUGDB";
n try{
n
Class.forName("com.mysql.jdbc.Driver");
n
con = DriverManager.getConnection(url,
n
user,pass);
n }catch(
Exception e ){
n
e.printStackTrace();
n }
n return
con;
n }
n
n public static void main( String args[]
)
n throws
Exception{
n String sql = "SELECT * FROM
BUGS";
n con = open();
n try {
n Statement st=con.createStatement();
n ResultSet rs= st.executeQuery(sql);
n while( rs.next() ){
n
System.out.println("Titulo: "+
n
rs.getString("titulo"));
n }
n
} catch (SQLException ex) {
n
ex.printStackTrace();
n
} finally{
n
con.close();
n
}
n
}
n}
nCREATE TABLE
bug (
n id_bug
int(11) NOT NULL auto_increment,
n titulo
varchar(60) NOT NULL,
n data
date default NULL,
n texto
text NOT NULL,
n
PRIMARY KEY (id_bug)
n);