Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 190: Internet E-Commerce Lecture 13: Database code.

Similar presentations


Presentation on theme: "CSE 190: Internet E-Commerce Lecture 13: Database code."— Presentation transcript:

1 CSE 190: Internet E-Commerce Lecture 13: Database code

2 ASP Database code (ADO) ADO: Microsoft standard for connecting to databases ADO = Active Data Objects API is always accessible from within ASP ADO also used from within application tier components (i.e. VB, C++ components)

3 ADO Accessing a DB with ADO 1.Create ADO connection 2.Open the DB connection 3.Create an ADO Recordset 4.Open the Recordset 5.Extract the fields from the recordset 6.Close the recordset 7.Close the connection

4 ADO: Connections DSN: Analogous to a URL for a database DSN = Data Source Name

5 ADO: Connecting without DSN <% set connection = Server.CreateObject( “ADODB.Connection” ) connection.Provider = “Microsoft.Jet.OLEDB.4.0” Connection.Open “c:/demos/northwind.mdb” %>

6 ADO: Connecting with DSN <% set connection = Server.CreateObject( “ADODB.Connection” ) connection.Open “Northwind” %>

7 ADO: Creating a DSN In Windows 2000, Settings -> Control Panel -> Administrative Tools - > ODBC Choose “System DSN”, and then “Add…” Enter any identifying string for the DSN Choose “Select…” to specify where to find the source DB file

8 ADO: Connection Object Properties: Methods: CommandTimeoutSeconds to wait for command to finish ConnectionStringString used to create connection (e.g. northwind) StateWhether connection is open or closed ExecuteExecute SQL query, command, or statement OpenOpens a connection CancelCancels a currently executing command CloseCloses the connection BeginTransBegins a transaction RollbackTransCancel changes in current transaction CommitTransCommits a transaction

9 ADO: Creating Recordset Recordset: A table of rows representing the results of a query or contents of an existing table Recordset via SQL query: <% set connection = Server.CreateObject( “ADODB.Connection” ) connection.Open “northwind” set rs = Server.CreateObject( “ADODB.Recordset” ) rs.Open( “Select * from Customers” ), connection %>

10 ADO Recordset: Accessing Data <% set connection = Server.CreateObject( "ADODB.Connection“ ) connection.Open “northwind” set rs=Server.CreateObject( "ADODB.recordset“ ) rs.Open "Select * from Customers", connection for each x in rs.fields response.write( x.name ) response.write( " = “ ) response.write( x.value ) next %>

11 ADO: Accessing Data from all rows <% set connection = Server.CreateObject( "ADODB.Connection“ ) connection.Open “northwind” set rs=Server.CreateObject( "ADODB.recordset“ ) rs.Open "Select * from Customers", connection do until rs.EOF for each x in rs.fields response.write( x.name ) response.write( " = “ ) response.write( x.value ) next response.write “ ” rs.MoveNext loop %>

12 ADO: Clean up <% set connection = Server.CreateObject( "ADODB.Connection“ ) connection.Open “northwind” set rs=Server.CreateObject( "ADODB.recordset“ ) rs.Open "Select * from Customers", connection do until rs.EOF for each x in rs.fields response.write( x.name ) response.write( " = “ ) response.write( x.value ) next response.write “ ” rs.MoveNext loop rs.close connection.close %>

13 JDBC JDBC: Java version of ODBC, providing same functionality as ADO ODBC: pre-ADO DB connect technology from Microsoft Using JDBC: 1.Load the JDBC-ODBC bridge 2.Connect to data source 3.Execute SQL command 4.Access Recordset 5.Clean up

14 JDBC: Loading ODBC bridge Must load Java driver to connect to database Two methods: 1.Specify driver class name in code: Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver“ ); 2.Specify driver via Java property: jdbc.drivers = sun.jdbc.odbc.JdbcOdbcDriver

15 JDBC: Connecting to DB import java.sql.*; public class MyTest { public static void main( String[] args ) { String DSN = “jdbc:odbc:somedsn”; Connection conn = null; conn = DriverManager.getConnection( DSN, “sa”, “” ); }

16 JDBC: Executing SQL import java.sql.*; public class MyTest { public static void main( String[] args ) { String DSN = “jdbc:odbc:somedsn”; Connection conn = null; Statement statement = null; conn = DriverManager.getConnection( DSN, “sa”, “” ); statement = conn.createStatement(); ResultSet result = statement.executeQuery( “SELECT programmer, cups FROM JoltData ORDER BY cups DESC;"); }

17 JDBC: Access Recordset import java.sql.*; public class MyTest { public static void main( String[] args ) { String DSN = “jdbc:odbc:somedsn”; Connection conn = null; Statement statement = null; conn = DriverManager.getConnection( DSN, “sa”, “” ); statement = conn.createStatement(); ResultSet result = statement.executeQuery( “SELECT programmer, cups FROM JoltData ORDER BY cups DESC;"); // for each row of data (note typed fields) while( result.next() ) { String name = result.getString( “programmer” ); int cups = result.getInt( “cups” ); }

18 JDBC: Clean up import java.sql.*; public class MyTest { public static void main( String[] args ) { String DSN = “jdbc:odbc:somedsn”; Connection conn = null; Statement statement = null; conn = DriverManager.getConnection( DSN, “sa”, “” ); statement = conn.createStatement(); ResultSet result = statement.executeQuery( “SELECT programmer, cups FROM JoltData ORDER BY cups DESC;"); // for each row of data (note typed fields) while( result.next() ) { String name = result.getString( “programmer” ); int cups = result.getInt( “cups” ); } // Clean up conn.close(); }

19 Perl DBI: Quick Overview use DBI; # Connect to DB my $dbh = DBI->connect( 'DBI:Oracle:payroll‘ ) or die "Couldn't connect to database: ". DBI->errstr; # Prepare SQL for execution my $sth = $dbh->prepare( 'SELECT * FROM people WHERE lastname = ?') or die "Couldn't prepare statement: ". $dbh->errstr; print "Enter name> "; while ($lastname = <>) { # Read input from the user my @data; chomp $lastname; $sth->execute( $lastname ) # Execute the query or die "Couldn't execute statement: ". $sth->errstr; # Fetch the record set while (@data = $sth->fetchrow_array()) { my $firstname = $data[1]; my $id = $data[2]; print "\t$id: $firstname $lastname\n"; } if ($sth->rows == 0) { print "No names matched `$lastname'.\n\n"; } # Clean up statement $sth->finish; print "\n"; print "Enter name> "; } # Clean up connection $dbh->disconnect;

20 References ASP –http://www.w3schools.com/ado/ado_intro.asp JDBC –http://developer.java.sun.com/developer/onlin eTraining/Database/JDBCShortCourse/jdbc/jd bc.html Perl –http://www.perl.com/pub/a/1999/10/DBI.html


Download ppt "CSE 190: Internet E-Commerce Lecture 13: Database code."

Similar presentations


Ads by Google