Base de dados

Maio 8, 2009

Tecnologia JPA:

http://www.coinfo.cefetpb.edu.br/professor/petronio/POD/Material/EJB3.0/Material/04%20-%20Persistencia%20Entity%20Bean%20Entity%20Manager.ppt

Exercício:

http://www.netbeans.org/kb/docs/java/gui-db-custom_pt_BR.html

Tecnlogia JDBC, exemplo:

package connectbd;
import java.sql.*;
/**
*
* @author professor
*/
public class acessoBD {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// Create a variable for the connection string.
String connectionUrl = “jdbc:sqlserver://portatil:1433;” +
“databaseName=Taloes;integratedSecurity=true;”;
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Establish the connection.
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some data.
String SQL = “SELECT * FROM dbo.Funcionarios”;
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getInt(1) + ” ” + rs.getString(2));
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}

package connectbd;

import java.sql.*;

/**

*

* @author professor

*/

public class acessoBD {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

// Create a variable for the connection string.

String connectionUrl = “jdbc:sqlserver://portatil:1433;” +

“databaseName=Taloes;integratedSecurity=true;”;

// Declare the JDBC objects.

Connection con = null;

Statement stmt = null;

ResultSet rs = null;

try {

// Establish the connection.

Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);

con = DriverManager.getConnection(connectionUrl);

// Create and execute an SQL statement that returns some data.

String SQL = “SELECT * FROM dbo.Funcionarios”;

stmt = con.createStatement();

rs = stmt.executeQuery(SQL);

// Iterate through the data in the result set and display it.

while (rs.next()) {

System.out.println(rs.getInt(1) + ” ” + rs.getString(2));

}

}

// Handle any errors that may have occurred.

catch (Exception e) {

e.printStackTrace();

}

finally {

if (rs != null) try { rs.close(); } catch(Exception e) {}

if (stmt != null) try { stmt.close(); } catch(Exception e) {}

if (con != null) try { con.close(); } catch(Exception e) {}

}

}

}

Deixar um comentário

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Modificar )

Imagem do Twitter

You are commenting using your Twitter account. Log Out / Modificar )

Facebook photo

You are commenting using your Facebook account. Log Out / Modificar )

Connecting to %s

Seguir

Get every new post delivered to your Inbox.