MySQL & JDCB Αλέξανδρος Καρακασίδης Δεκέμβριος 2004.

Slides:



Advertisements
Similar presentations
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Advertisements

JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
1 JDBC: Part I Attribution These slides are based on three primary sources: –Sun JDBC Tutorial URL: jdbc/TOC.htmlhttp://java.sun.com/docs/books/tutorial/
JDBC. JDBC (Java Database Connectivity): JDBC is an API for the Java programming language that defines how a client may access a database. It provides.
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
Advanced Java Programming – Eran Toch Methodologies in Information System Development Tutorial: Advanced Java Programming and Database connection Eran.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
Introduction to JDBC (Java Database Connectivity).
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
Think Possibility Integrating Web Applications With Databases.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
JDBC and Hibernate Joshua Scotton. Connecting to Relational DBs.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
CS 405G: Introduction to Database Systems Database programming.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Object-Oriented Software How does it differ from procedural? How is it similar to procedural? Why has it become so popular? Does it replace or supplement.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
Chapter 8 Databases.
JDBC. A Basic MySQL Tutorial MySQL is an open source database management software that helps users store, organize, and retrieve data. It is a very powerful.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
JDBC Establish a connection with a database or access any tabular data source Send SQL statements Process the results Two major sets of interfaces: JDBC.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
EXAMPLE I An application showing JDBC access to Cloudscape.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Tasks Needed for MissionMapEditor Martin Q. Zhao September 18, 2010.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang.
JDBC Java DataBase Connectivity. Loading the driver import java.sql.*... Class.forName("org.postgresql.Driver")‏
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Web Programming Assistant Professor Xiaozhong Liu
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Database Programming Basic JDBC Programming Concepts.
CompSci 280 S Introduction to Software Development
CS JDBC.
Lec - 14.
JDBC – Java Database Connectivity
JAVA Connection The following uses a ‘bridge’ between Java Database Connectivity (JDBC) and ODBC, namely sun.jdbc.odbc.JdbcOdbcDriver Supplied with the.
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
Super Market Management
Bolat Azamat, Kim Dongmin
JDBC Example.
Presentation transcript:

MySQL & JDCB Αλέξανδρος Καρακασίδης Δεκέμβριος 2004

// Main Class. It also inherits from JFrame to open a window public class Example1 extends JFrame{ // Connect to the database test, at server localhost static String url="jdbc:mysql://localhost/test"; // The connection object. This holds all connection information static Connection con; … }

Main Function Example1(login,pass); // Constructor – Connection Initialization app.createTable(); // Creation of the Table in the Database app.insertData(); // Data Insertion app.getData(); // Data Selection and Display in a Window app.dropTable(); // Creation of the Table in the Database

Connection Initialization public void myinit(String login, String pass){ //Driver to be added at: $JAVA_HOME/jre/lib/ext/ //Driver refers to a.jar file, e.g., mysql-connector-java stable-bin.jar // Load the Driver by adding the proper path inside the jar try { Class.forName("com.mysql.jdbc.Driver"); //Class.forName("org.gjt.mm.mysql.Driver"); } catch (java.lang.ClassNotFoundException e) {...} // Connect to the Database try { con = DriverManager.getConnection (url, login, pass); } catch(SQLException ex) {...} }

public void createTable() Statement stmt; // Used to send the SQL Query through the connection to the DBMS // Query to create the table String query="CREATE TABLE `partsupp` ( PS_PARTKEY` int(11) NOT NULL default '0',"+ "`PS_SUPPKEY` int(11) NOT NULL default '0', `PS_AVAILQTY` int(11) NOT NULL default '0',"+ "`PS_SUPPLYCOST` decimal(10,0) NOT NULL default '0',`PS_COMMENT` varchar(199) NOT NULL default '') COMMENT='TPC-H partsup table'"; try // Connect to the DBMS and send the query { stmt = con.createStatement(); // Create the statement stmt.executeUpdate(query); // Execute UPDATE. CAUTION! It is an update! stmt.close(); // Close the statement } catch(SQLException ex){ System.err.println("SQLException: "+ex.getMessage()); }

public void insertData() (1/2) public void insertData() { Statement stmt; // Used to send the SQL Query through the connection to the DBMS String data= // Data to insert "2|3|8895|378.49|furiously_even_asymptotes_are_furiously_regular_plate |\n“ +…. + "2|939|3025|306.39|deposits_according_to_the_final,_special_foxes_dete c|\n"; // Separate the rows StringTokenizer rowt = new StringTokenizer(data,"\n"); …

public void insertData() (2/2) int num_rows = rowt.countTokens(); // Count rows try{ for(int j=0; j<num_rows; j++){ row = rowt.nextToken(); // Take each row StringTokenizer st = new StringTokenizer(row,"|"); // Separate Fields for(int i=0; i<5; i++) {mytoken[i] = st.nextToken();} // we have 5 fields query ="INSERT INTO `partsupp` VALUES ('"+ // Query to insert data mytoken[0]+"','"+mytoken[1]+"','"+mytoken[2]+"','"+mytoken[3]+"','"+mytoken [4]+"')"; stmt.executeUpdate(query); // Execute UPDATE. CAUTION! It is an update! } stmt.close(); } catch(SQLException ex) {…}}

public void getData() ResultSet res; // The cursor that holds the result String query="SELECT PS_AVAILQTY,PS_SUPPLYCOST FROM `partsupp`"; try {stmt = con.createStatement(); // Execute QUERY. CAUTION! It is a query returning results! res = stmt.executeQuery(query); while( res.next() ) { // Extract each result row and print it on screen qty = res.getInt("PS_AVAILQTY"); cost = res.getInt("PS_SUPPLYCOST"); System.out.println(qty+":"+cost); } // End while stmt.close(); } catch(SQLException ex) {..}

public void dropTable() // Can You Explain it? Statement stmt; String query="DROP TABLE `partsupp`"; try{ stmt = con.createStatement(); stmt.executeUpdate(query); stmt.close(); } catch(SQLException ex) { System.err.println("SQLException: "+ex.getMessage()); }