SlideShare a Scribd company logo
Basic JDBC Programming Concepts
Database URLs For databases on the Internet/intranet, the  subname  can contain the Net URL  //hostname:port/…  The < subprotocol > can be any name that a database understands. The odbc subprotocol name is reserved for ODBC-style data sources. A normal ODBC database JDBC URL looks like:  jdbc:odbc:<>;User=<>;PW=<>   jdbc:postgresql ://www.hcmuaf.edu.vn/ts
Making the Connection Loading a driver: Class.forName(className) Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;); System.setProperty(“jdbc.drivers”, “className”); System.setProperty(“jdbc.drivers”, “sun.jdbc.odbc.JdbcOdbcDriver”); Making a connection Connection conn = DriverManager.getConnection(…); static Connection getConnection(String url); static Connection getConnection(String url,  String user,String password); static int getLoginTimeout(); // seconds static void setLoginTimeout(int seconds); static void println(String message);// in ra logfile static void registerDriver(Driver d); static void deregisterDriver(Driver d);
Executing SQL Commands The  Statement  object does all of the work to interact with the Database Management System in terms of SQL statements. You can create many  Statement  objects from one  Connection  object.  JDBC supports three types of statements:  Statement  : the SQL is prepared and executed in one step (from the application program point of view)  PreparedStatement : the driver stores the execution plan handle for later use (SQL command with a parameters).  CallableStatement : the SQL statement is actually making a call to a stored procedure  The  Connection  object has the  createStatement (),  prepareStatement (), and  prepareCall () methods to create these  Statement  objects.
Creating and Using Direct SQL Statements  A  Statement  object is created using the  createStatement () method in the  Connection  object obtained from the call to  DriverManager.getConnection resultSet executeQuery (String sql) int executeUpdate (String sql) boolean execute(String sql) The  executeUpdate  method can execute actions such as  INSERT ,  UPDATE , and  DELETE  as well as data definition commands such as  CREATE   TABLE , and  DROPTABLE . The  executeUpdate  method returns a count of the rows that were affected by the SQL command. The  executeQuery  method is used to execute  SELECT  queries. The  executeQuery  object returns an object of type  ResultSet  that you use to walk through the result a row at a time. ResultSet rs = stat.executeQuery(&quot;SELECT * FROM Books&quot;)
Using executeUpdate method try  { Class. forName ( &quot;sun.jdbc.odbc.JdbcOdbcDriver&quot; ); Connection  connection  =  DriverManager . getConnection (url,userName,password); Statement  statement  = connection.createStatement(); String sqlCommand = &quot;CREATE TABLE Books(Title CHAR(60),&quot; +  &quot;ISBN CHAR(13),Price CURRENCY)&quot; ; statement.executeUpdate (sqlCommand); sqlCommand = &quot; INSERT INTO Books VALUES(&quot;  + &quot;'Beyond HTML', '0-07-882198-3', 27.95)&quot; ; statement.executeUpdate (sqlCommand);  statement.close (); connection.close (); }  catch  (Exception e) { System. out .println( e.getMessage() ); }
Using  executeQuery method try  { Class. forName ( &quot;sun.jdbc.odbc.JdbcOdbcDriver&quot; ); Connection connection =  DriverManager. getConnection (url, userName,password); Statement statement = connection.createStatement(); String sqlCommand =  &quot;SELECT * FROM BOOKS&quot; ; ResultSet rs = statement.executeQuery(sqlCommand); while  (rs.next()) { String title = rs.getString(1); String isbn = rs.getString(2); float  price = rs.getFloat( &quot;Price&quot; ); System. out .println(title +  &quot;, &quot;  + isbn +  &quot; , &quot;  + price); } rs.close(); }  catch  (Exception e){};
SQL data types and corresponding Java types java.sql.Time TIME java.sql.Date DATE  Boolean BOOLEAN  String VARCHAR( n ) String CHARACTER( n ) or  CHAR( n ) double DOUBLE float REAL double FLOAT(n)  java.sql.Numeric NUMERIC( m,n ), DECIMAL( m,n )or DEC( m,n ) short SMALLINT int INTEGER or INT Java data type SQL data type
Creating and Using PreparedStatement The  PreparedStatement , the driver actually sends only the execution plan ID and the parameters to the DBMS. This results in less network traffic and is well-suited for Java applications on the Internet. The  PreparedStatement   should be used when you need to execute the SQL statement many times in a Java application . The DBMS discards the execution plan at the end of the program. So, the DBMS must go through all of the steps of creating an execution plan every time the program runs.  The PreparedStatement object achieves faster SQL execution performance than the simple Statement object , as the DBMS does not have to run through the steps of creating the execution plan.  Notice that the  executeQuery (),  executeUpdate (), and  execute () methods do not take any parameters.  Return Type Method Name   Parameter ResultSet   executeQuery   ( ) int executeUpdate   ( ) Boolean   execute   ( )
Creating and Using PreparedStatement One of the major features of a  PreparedStatement  is that it can handle  IN  types of  parameters . The parameters are indicated in a  SQL  statement by placing the  ?  as the parameter marker instead of the actual values. In the Java program, the association is made to the parameters with the  setXXXX ()  methods. All of the  setXXXX ()  methods take the parameter index, which is  1  for the first &quot; ? ,&quot;  2  for the second &quot; ? ,&quot;and so on. void setBoolean  (int parameterIndex, boolean x)  void setByte  (int parameterIndex, byte x)  void setDouble  (int parameterIndex, double x)  void setFloat  (int parameterIndex, float x)  void setInt  (int parameterIndex, int x)  void setLong  (int parameterIndex, long x)  void setNumeric  (int parameterIndex, Numeric x)  void setShort  (int parameterIndex, short x)  void setString  (int parameterIndex, String x)
Creating and Using PreparedStatement try { Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;); Connection connection =      DriverManager.getConnection(url,userName,password); PrepStmt = connection.prepareStatement(&quot;SELECT * FROM  BOOKS WHERE ISBN =  ? &quot;); PrepStmt.setString(1,&quot;0-07-882198-3&quot;); ResultSet rs = PrepStmt.executeQuery () ; while (rs.next()) { title = rs.getString(1); isbn = rs.getString(2); price = rs.getFloat(&quot;Price&quot;); System.out.println(title + &quot;, &quot; + isbn + &quot; , &quot; + price ); } rs.close(); PrepStmt.close(); connection.close(); }
java.sql.DriverManager static Connection getConnection(String url, String user,String password) establishes a connection to the given database and returns a Connection object.
java.sql.Connection Statement createStatement() creates a statement object that can be used to execute SQL queries and updates without parameters. PreparedStatement   prepareStatement(String   sql ) returns a  PreparedStatement   object containing the precompiled statement. The string  sql   contains a SQL statement that can contain one or more parameter placeholders denoted by  ?   characters. void close() immediately closes the current connection.
java.sql.Statement ResultSet executeQuery(String sql) executes the SQL statement given in the string and returns a  ResultSet  to view the query result. int executeUpdate(String sql) executes the SQL  INSERT ,  UPDATE , or  DELETE  statement specified by the string. Also used to execute Data Definition Language (DDL) statements such as  CREATE   TABLE . Returns the number of records affected, or -1 for a statement without an update count. boolean execute(String sql) executes the SQL statement specified by the string. Returns  true   if the statement returns a result set,  false   otherwise. Use the  getResultSet   or  getUpdateCount   method to obtain the statement outcome. int getUpdateCount() Returns the number of records affected by the preceding update statement, or -1 if the preceding statement was a statement without an update count. Call this method only once per executed statement. ResultSet getResultSet() Returns the result set of the preceding query statement, or null if the preceding statement did not have a result set. Call this method only once per executed statement.
java.sql.ResultSet boolean next() makes the current row in the result set move forward by one. Returns  false   after the last row. Note that you must call this method to advance to the first row. xxx getXxx(int columnNumber) xxx getXxx(String columnName) ( xxx   is a type such as  int ,  double ,  String ,  Date , etc.) return the value of the column with column index  columnNumber   or with column names, converted to the specified type. Not all type conversions are legal. See documentation for details. int findColumn(String columnName)   gives the column index associated with a column name. void close()  immediately closes the current result set.
java.sql.PreparedStatement void set Xxx (int n,  Xxx  x) ( Xxx   is a type such as  int ,  double ,  String ,  Date , etc.) sets the value of the  n-th  parameter to  x . void clearParameters() clears all current parameters in the prepared statement. ResultSet executeQuery() executes a prepared SQL query and returns a  ResultSet   object. int executeUpdate() executes the prepared SQL  INSERT ,  UPDATE , or  DELETE   statement represented by the  PreparedStatement   object. Returns the number of rows affected, or 0 for DDL statements.

More Related Content

PDF
Session06 handling xml data
kendyhuu
 
PPT
Executing Sql Commands
leminhvuong
 
PDF
spring-tutorial
Arjun Shanka
 
PPT
Sqlapi0.1
jitendral
 
PDF
Advanced java practical semester 6_computer science
Niraj Bharambe
 
PPT
30 5 Database Jdbc
phanleson
 
PPT
Scrollable Updatable
phanleson
 
PPT
Scrollable Updatable
leminhvuong
 
Session06 handling xml data
kendyhuu
 
Executing Sql Commands
leminhvuong
 
spring-tutorial
Arjun Shanka
 
Sqlapi0.1
jitendral
 
Advanced java practical semester 6_computer science
Niraj Bharambe
 
30 5 Database Jdbc
phanleson
 
Scrollable Updatable
phanleson
 
Scrollable Updatable
leminhvuong
 

What's hot (20)

PDF
Lecture17
vantinhkhuc
 
ODP
Java Persistence API
Carol McDonald
 
PPTX
Jdbc ja
DEEPIKA T
 
PPTX
JPA 2.0
Emiel Paasschens
 
PPT
Jdbc oracle
yazidds2
 
PPT
Chapter 4 - Defining Your Own Classes - Part I
Eduardo Bergavera
 
PDF
Selectors and normalizing state shape
Muntasir Chowdhury
 
PPT
JDBC
Sunil OS
 
PPTX
Oracle 10g
Svetlin Nakov
 
PPTX
JDBC - JPA - Spring Data
Arturs Drozdovs
 
PPTX
Structural pattern 3
Naga Muruga
 
DOC
Converting Db Schema Into Uml Classes
Kaniska Mandal
 
PPT
XQuery Triggers in Native XML Database Sedna
maria.grineva
 
PDF
Ejb3 Dan Hinojosa
Dan Hinojosa
 
PPT
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
PPT
Refactoring Jdbc Programming
chanwook Park
 
PPTX
The Tools for Data Migration Between Oracle , MySQL and Flat Text File.
anysql
 
DOCX
Diifeerences In C#
rohit_gupta_mrt
 
PDF
Jdbc tutorial
Dharma Kshetri
 
PDF
Simple Jdbc With Spring 2.5
David Motta Baldarrago
 
Lecture17
vantinhkhuc
 
Java Persistence API
Carol McDonald
 
Jdbc ja
DEEPIKA T
 
Jdbc oracle
yazidds2
 
Chapter 4 - Defining Your Own Classes - Part I
Eduardo Bergavera
 
Selectors and normalizing state shape
Muntasir Chowdhury
 
JDBC
Sunil OS
 
Oracle 10g
Svetlin Nakov
 
JDBC - JPA - Spring Data
Arturs Drozdovs
 
Structural pattern 3
Naga Muruga
 
Converting Db Schema Into Uml Classes
Kaniska Mandal
 
XQuery Triggers in Native XML Database Sedna
maria.grineva
 
Ejb3 Dan Hinojosa
Dan Hinojosa
 
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
Refactoring Jdbc Programming
chanwook Park
 
The Tools for Data Migration Between Oracle , MySQL and Flat Text File.
anysql
 
Diifeerences In C#
rohit_gupta_mrt
 
Jdbc tutorial
Dharma Kshetri
 
Simple Jdbc With Spring 2.5
David Motta Baldarrago
 
Ad

Viewers also liked (7)

PPTX
Creating database using sql commands
Belle Wx
 
PPS
Execute MySQL query using command prompt
Ikhwan Krisnadi
 
PPSX
Execute sql query or sql command sql server using command prompt
Ikhwan Krisnadi
 
PDF
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
Denodo
 
PPT
SQL Tutorial - Basic Commands
1keydata
 
PPT
Sql ppt
Anuja Lad
 
PPT
ASP.NET Tutorial - Presentation 1
Kumar S
 
Creating database using sql commands
Belle Wx
 
Execute MySQL query using command prompt
Ikhwan Krisnadi
 
Execute sql query or sql command sql server using command prompt
Ikhwan Krisnadi
 
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
Denodo
 
SQL Tutorial - Basic Commands
1keydata
 
Sql ppt
Anuja Lad
 
ASP.NET Tutorial - Presentation 1
Kumar S
 
Ad

Similar to Executing Sql Commands (20)

PPT
JDBC – Java Database Connectivity
Information Technology
 
PPT
Jdbc
smvdurajesh
 
PPT
JDBC for CSQL Database
jitendral
 
PPT
JDBC Tutorial
Information Technology
 
PDF
Jdbc[1]
Fulvio Corno
 
PDF
JDBC programming
Fulvio Corno
 
PPTX
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
tirthasurani118866
 
PPT
jdbc
vikram singh
 
PPS
Jdbc api
kamal kotecha
 
PPT
CS124-L9-JDBC.ppt Add more information to your upload
muzammil9676
 
PPT
JDBC JAVA DATABASE CONNECTIVITY AND JAVA
AdarshSrungarapu
 
PPT
Data Access with JDBC
BG Java EE Course
 
PPT
3 database-jdbc(1)
hameedkhan2017
 
PPT
4. Database Connectivity using JDBC .ppt
HITENKHEMANI
 
PDF
Java 1-contd
Mukesh Tekwani
 
PDF
Introduction to JDBC and database access in web applications
Fulvio Corno
 
PPTX
Database Access With JDBC
Dharani Kumar Madduri
 
PPT
JDBC Connecticity.ppt
Swapnil Kale
 
PPT
JDBC.ppt JDBC_FM_2012_201JDBC_FM_2012_201
rorebik626
 
PPT
JDBC_Template for database connection using Spring
gangishettysaikrishn
 
JDBC – Java Database Connectivity
Information Technology
 
JDBC for CSQL Database
jitendral
 
JDBC Tutorial
Information Technology
 
Jdbc[1]
Fulvio Corno
 
JDBC programming
Fulvio Corno
 
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
tirthasurani118866
 
Jdbc api
kamal kotecha
 
CS124-L9-JDBC.ppt Add more information to your upload
muzammil9676
 
JDBC JAVA DATABASE CONNECTIVITY AND JAVA
AdarshSrungarapu
 
Data Access with JDBC
BG Java EE Course
 
3 database-jdbc(1)
hameedkhan2017
 
4. Database Connectivity using JDBC .ppt
HITENKHEMANI
 
Java 1-contd
Mukesh Tekwani
 
Introduction to JDBC and database access in web applications
Fulvio Corno
 
Database Access With JDBC
Dharani Kumar Madduri
 
JDBC Connecticity.ppt
Swapnil Kale
 
JDBC.ppt JDBC_FM_2012_201JDBC_FM_2012_201
rorebik626
 
JDBC_Template for database connection using Spring
gangishettysaikrishn
 

More from phanleson (20)

PDF
Learning spark ch01 - Introduction to Data Analysis with Spark
phanleson
 
PPT
Firewall - Network Defense in Depth Firewalls
phanleson
 
PPT
Mobile Security - Wireless hacking
phanleson
 
PPT
Authentication in wireless - Security in Wireless Protocols
phanleson
 
PPT
E-Commerce Security - Application attacks - Server Attacks
phanleson
 
PPT
Hacking web applications
phanleson
 
PPTX
HBase In Action - Chapter 04: HBase table design
phanleson
 
PPT
HBase In Action - Chapter 10 - Operations
phanleson
 
PPT
Hbase in action - Chapter 09: Deploying HBase
phanleson
 
PPTX
Learning spark ch11 - Machine Learning with MLlib
phanleson
 
PPTX
Learning spark ch10 - Spark Streaming
phanleson
 
PPTX
Learning spark ch09 - Spark SQL
phanleson
 
PPT
Learning spark ch07 - Running on a Cluster
phanleson
 
PPTX
Learning spark ch06 - Advanced Spark Programming
phanleson
 
PPTX
Learning spark ch05 - Loading and Saving Your Data
phanleson
 
PPTX
Learning spark ch04 - Working with Key/Value Pairs
phanleson
 
PPTX
Learning spark ch01 - Introduction to Data Analysis with Spark
phanleson
 
PPT
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
phanleson
 
PPT
Lecture 1 - Getting to know XML
phanleson
 
PPTX
Lecture 4 - Adding XTHML for the Web
phanleson
 
Learning spark ch01 - Introduction to Data Analysis with Spark
phanleson
 
Firewall - Network Defense in Depth Firewalls
phanleson
 
Mobile Security - Wireless hacking
phanleson
 
Authentication in wireless - Security in Wireless Protocols
phanleson
 
E-Commerce Security - Application attacks - Server Attacks
phanleson
 
Hacking web applications
phanleson
 
HBase In Action - Chapter 04: HBase table design
phanleson
 
HBase In Action - Chapter 10 - Operations
phanleson
 
Hbase in action - Chapter 09: Deploying HBase
phanleson
 
Learning spark ch11 - Machine Learning with MLlib
phanleson
 
Learning spark ch10 - Spark Streaming
phanleson
 
Learning spark ch09 - Spark SQL
phanleson
 
Learning spark ch07 - Running on a Cluster
phanleson
 
Learning spark ch06 - Advanced Spark Programming
phanleson
 
Learning spark ch05 - Loading and Saving Your Data
phanleson
 
Learning spark ch04 - Working with Key/Value Pairs
phanleson
 
Learning spark ch01 - Introduction to Data Analysis with Spark
phanleson
 
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
phanleson
 
Lecture 1 - Getting to know XML
phanleson
 
Lecture 4 - Adding XTHML for the Web
phanleson
 

Executing Sql Commands

  • 2. Database URLs For databases on the Internet/intranet, the subname can contain the Net URL //hostname:port/… The < subprotocol > can be any name that a database understands. The odbc subprotocol name is reserved for ODBC-style data sources. A normal ODBC database JDBC URL looks like: jdbc:odbc:<>;User=<>;PW=<> jdbc:postgresql ://www.hcmuaf.edu.vn/ts
  • 3. Making the Connection Loading a driver: Class.forName(className) Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;); System.setProperty(“jdbc.drivers”, “className”); System.setProperty(“jdbc.drivers”, “sun.jdbc.odbc.JdbcOdbcDriver”); Making a connection Connection conn = DriverManager.getConnection(…); static Connection getConnection(String url); static Connection getConnection(String url, String user,String password); static int getLoginTimeout(); // seconds static void setLoginTimeout(int seconds); static void println(String message);// in ra logfile static void registerDriver(Driver d); static void deregisterDriver(Driver d);
  • 4. Executing SQL Commands The Statement object does all of the work to interact with the Database Management System in terms of SQL statements. You can create many Statement objects from one Connection object. JDBC supports three types of statements: Statement : the SQL is prepared and executed in one step (from the application program point of view) PreparedStatement : the driver stores the execution plan handle for later use (SQL command with a parameters). CallableStatement : the SQL statement is actually making a call to a stored procedure The Connection object has the createStatement (), prepareStatement (), and prepareCall () methods to create these Statement objects.
  • 5. Creating and Using Direct SQL Statements A Statement object is created using the createStatement () method in the Connection object obtained from the call to DriverManager.getConnection resultSet executeQuery (String sql) int executeUpdate (String sql) boolean execute(String sql) The executeUpdate method can execute actions such as INSERT , UPDATE , and DELETE as well as data definition commands such as CREATE TABLE , and DROPTABLE . The executeUpdate method returns a count of the rows that were affected by the SQL command. The executeQuery method is used to execute SELECT queries. The executeQuery object returns an object of type ResultSet that you use to walk through the result a row at a time. ResultSet rs = stat.executeQuery(&quot;SELECT * FROM Books&quot;)
  • 6. Using executeUpdate method try { Class. forName ( &quot;sun.jdbc.odbc.JdbcOdbcDriver&quot; ); Connection connection = DriverManager . getConnection (url,userName,password); Statement statement = connection.createStatement(); String sqlCommand = &quot;CREATE TABLE Books(Title CHAR(60),&quot; + &quot;ISBN CHAR(13),Price CURRENCY)&quot; ; statement.executeUpdate (sqlCommand); sqlCommand = &quot; INSERT INTO Books VALUES(&quot; + &quot;'Beyond HTML', '0-07-882198-3', 27.95)&quot; ; statement.executeUpdate (sqlCommand); statement.close (); connection.close (); } catch (Exception e) { System. out .println( e.getMessage() ); }
  • 7. Using executeQuery method try { Class. forName ( &quot;sun.jdbc.odbc.JdbcOdbcDriver&quot; ); Connection connection = DriverManager. getConnection (url, userName,password); Statement statement = connection.createStatement(); String sqlCommand = &quot;SELECT * FROM BOOKS&quot; ; ResultSet rs = statement.executeQuery(sqlCommand); while (rs.next()) { String title = rs.getString(1); String isbn = rs.getString(2); float price = rs.getFloat( &quot;Price&quot; ); System. out .println(title + &quot;, &quot; + isbn + &quot; , &quot; + price); } rs.close(); } catch (Exception e){};
  • 8. SQL data types and corresponding Java types java.sql.Time TIME java.sql.Date DATE Boolean BOOLEAN String VARCHAR( n ) String CHARACTER( n ) or CHAR( n ) double DOUBLE float REAL double FLOAT(n) java.sql.Numeric NUMERIC( m,n ), DECIMAL( m,n )or DEC( m,n ) short SMALLINT int INTEGER or INT Java data type SQL data type
  • 9. Creating and Using PreparedStatement The PreparedStatement , the driver actually sends only the execution plan ID and the parameters to the DBMS. This results in less network traffic and is well-suited for Java applications on the Internet. The PreparedStatement should be used when you need to execute the SQL statement many times in a Java application . The DBMS discards the execution plan at the end of the program. So, the DBMS must go through all of the steps of creating an execution plan every time the program runs. The PreparedStatement object achieves faster SQL execution performance than the simple Statement object , as the DBMS does not have to run through the steps of creating the execution plan. Notice that the executeQuery (), executeUpdate (), and execute () methods do not take any parameters. Return Type Method Name Parameter ResultSet executeQuery ( ) int executeUpdate ( ) Boolean execute ( )
  • 10. Creating and Using PreparedStatement One of the major features of a PreparedStatement is that it can handle IN types of parameters . The parameters are indicated in a SQL statement by placing the ? as the parameter marker instead of the actual values. In the Java program, the association is made to the parameters with the setXXXX () methods. All of the setXXXX () methods take the parameter index, which is 1 for the first &quot; ? ,&quot; 2 for the second &quot; ? ,&quot;and so on. void setBoolean (int parameterIndex, boolean x) void setByte (int parameterIndex, byte x) void setDouble (int parameterIndex, double x) void setFloat (int parameterIndex, float x) void setInt (int parameterIndex, int x) void setLong (int parameterIndex, long x) void setNumeric (int parameterIndex, Numeric x) void setShort (int parameterIndex, short x) void setString (int parameterIndex, String x)
  • 11. Creating and Using PreparedStatement try { Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;); Connection connection = DriverManager.getConnection(url,userName,password); PrepStmt = connection.prepareStatement(&quot;SELECT * FROM BOOKS WHERE ISBN = ? &quot;); PrepStmt.setString(1,&quot;0-07-882198-3&quot;); ResultSet rs = PrepStmt.executeQuery () ; while (rs.next()) { title = rs.getString(1); isbn = rs.getString(2); price = rs.getFloat(&quot;Price&quot;); System.out.println(title + &quot;, &quot; + isbn + &quot; , &quot; + price ); } rs.close(); PrepStmt.close(); connection.close(); }
  • 12. java.sql.DriverManager static Connection getConnection(String url, String user,String password) establishes a connection to the given database and returns a Connection object.
  • 13. java.sql.Connection Statement createStatement() creates a statement object that can be used to execute SQL queries and updates without parameters. PreparedStatement prepareStatement(String sql ) returns a PreparedStatement object containing the precompiled statement. The string sql contains a SQL statement that can contain one or more parameter placeholders denoted by ? characters. void close() immediately closes the current connection.
  • 14. java.sql.Statement ResultSet executeQuery(String sql) executes the SQL statement given in the string and returns a ResultSet to view the query result. int executeUpdate(String sql) executes the SQL INSERT , UPDATE , or DELETE statement specified by the string. Also used to execute Data Definition Language (DDL) statements such as CREATE TABLE . Returns the number of records affected, or -1 for a statement without an update count. boolean execute(String sql) executes the SQL statement specified by the string. Returns true if the statement returns a result set, false otherwise. Use the getResultSet or getUpdateCount method to obtain the statement outcome. int getUpdateCount() Returns the number of records affected by the preceding update statement, or -1 if the preceding statement was a statement without an update count. Call this method only once per executed statement. ResultSet getResultSet() Returns the result set of the preceding query statement, or null if the preceding statement did not have a result set. Call this method only once per executed statement.
  • 15. java.sql.ResultSet boolean next() makes the current row in the result set move forward by one. Returns false after the last row. Note that you must call this method to advance to the first row. xxx getXxx(int columnNumber) xxx getXxx(String columnName) ( xxx is a type such as int , double , String , Date , etc.) return the value of the column with column index columnNumber or with column names, converted to the specified type. Not all type conversions are legal. See documentation for details. int findColumn(String columnName) gives the column index associated with a column name. void close() immediately closes the current result set.
  • 16. java.sql.PreparedStatement void set Xxx (int n, Xxx x) ( Xxx is a type such as int , double , String , Date , etc.) sets the value of the n-th parameter to x . void clearParameters() clears all current parameters in the prepared statement. ResultSet executeQuery() executes a prepared SQL query and returns a ResultSet object. int executeUpdate() executes the prepared SQL INSERT , UPDATE , or DELETE statement represented by the PreparedStatement object. Returns the number of rows affected, or 0 for DDL statements.