Database Systems - Java Database Programming with MySQL and Connector/J - Using Query Parameters
Connector/J is a MySQL component based on the JDBC standard from Oracle Setup your Java program for database programming: Import the Connector/J jar file in...
🔥 Related Trending Topics
LIVE TRENDSThis video may be related to current global trending topics. Click any trend to explore more videos about what's hot right now!
THIS VIDEO IS TRENDING!
This video is currently trending in Bangladesh under the topic 's'.
About this video
Connector/J is a MySQL component based on the JDBC standard from Oracle
Setup your Java program for database programming:
Import the Connector/J jar file into your Java project and configure your classpath
Import java.sql.Connection and java.sql.DriverManager
Connect with DriverManager.getConnection()within a try-catch statement
Close the connection with connection.close()
Connector/J Link: https://dev.mysql.com/downloads/connector/j/
Executing queries
createStatement() – creates a Statement object that is associated to the database connection. Statement is an interface used for executing SQL queries. Import it in your Java code with import java.sql.Statement;.
executeQuery() - statement method that compiles the SELECT query into low-level databse operations and then executes the query. Pass the string query as the parameter, and it returns a ResultSet object which contains the results of the query
executeUpdate() - executes queries that update data such as INSERT, UPDATE, and DELETE. It returns the number of matched query rows.
execute() - executes any SQL statements, and can even be used for CREATE TABLE statements. It returns a boolean value true if a ResultSet object is generated.
getResultSet() – accesses the ResultSet generated by the execute() method.
close() - closes and releases statement object. Call this method before you close any database connection.
Fetching values
The ResultSet interface gets query results.
executeQuery() - returns a ResultSet object with the query results. Each ResultSet object maintains a pointer to the current row of the query result. The first row is the initial row, and the next() method movies it to the next row and also returns true if the current row advances, or false if you’re at the last row of the query result.
ResultSet methods can access or fetch values from the result set:
getInt() takes a column name parameter and returns the integer value of the column.
getDouble() takes a column name parameter and returns the Double value of the column.
getString() takes a column name parameter and returns the String value of the column.
Other ResultSet methods return additional information:
getMetaData() returns a ResultSetMetaData object, containing information about the columns of a ResultSet object.
getWarnings() returns the first warning generated by a query.
The Result interface must be imported to a Java program with import java.sql.ResultSet;.
Query parameters
PreparedStatement - interface that extends the Statement interface and adds support for SQL query parameters.
prepareStatement() – creates PreparedStatment objects with the Connection interface, by passing a SQL query String parameter.
PreparedStatement inherits the executeQuery() and executeUpdate() methods from Statement, but they do not take a query parameter because prepared statements are assigned a query when they are created.
The SQL query can contains one or more ? characters to represent query parameters. Use PreparedStatement methods such as setString() and setInt() assign values to placeholders.
Ex: preparedStatement.setString(1, movieID) sets the first ? in the preparedStatement object to the String value of movieID.
It’s better to use placeholders with PreparedStatement rather than using Statement, because it could prevent a SQL injection attack, where a hacker can try to change the values of the SQL statements.
Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!
Video Information
Views
198
Total views since publication
Likes
7
User likes and reactions
Duration
7:47
Video length
Published
Sep 22, 2021
Release date
Quality
hd
Video definition
About the Channel
Tags and Topics
This video is tagged with the following topics. Click any tag to explore more related content and discover similar videos:
#appficial #java jdbc #java database programming #java database #connector/j #java mysql #java connect to database #java sql #java sql insert #java sql create table #java create table #preparedstatement #java statement #executeQuery() #java resultset #java #database query parameters #sql injection #Database tutorial #sql tutorial #database systems #database programming #sql course #database course #relational databases #free database #free sql #sql #learn sql #learn databases
Tags help categorize content and make it easier to find related videos. Browse our collection to discover more content in these categories.