SQLException: this checked exception is declared to be thrown by all the above methods, so we have to catch this exception explicitly when calling the above classes’ methods.Ĥ.The column value can be retrieved either by index number (1-based) or by column name. getString(), getInt(), getFloat() and so on). Use this object to iterate over rows in the result set using next() method, and get value of a column in the current row using getXXX() methods (e.g. ResultSet: contains table data returned by a SELECT query.Here the value of user_id is parameterized by a question mark and will be set by one of the setXXX() methods from the PreparedStatement interface, e.g. ResultSet executeQuery(String sql): executes a SELECT statement and returns a ResultSet object which contains results returned by the query.Ī prepared statement is one that contains placeholders (in form question marks ?) for dynamic values will be set at runtime.1 row inserted, or 2 rows updated, or 0 rows affected). int executeUpdate(String sql): executes an INSERT, UPDATE or DELETE statement and returns an update account indicating number of rows affected (e.g.This method can be used with a Statement only. ![]() ![]() It returns true if the query returns a ResultSet, false if the query returns an update count or returns nothing. boolean execute(String sql): executes a general SQL statement.Statement is the super interface of the PreparedStatement interface. Statement and PreparedStatement: these interfaces are used to execute static SQL query and parameterized SQL query, respectively.Connection: this interface represents an established database connection (session) from which we can create statements to execute queries and retrieve results, get metadata about the database, close connection, etc.MySQL in this tutorial) and to establish a database connection with the server via its getConnection() method. DriverManager: this class is used to register driver for a specific database type (e.g.They are all available under the java.sql package: Understand the main JDBC interfaces and classesLet’s take an overview look at the JDBC’s main interfaces and classes with which we usually work. Source Path\To\The\Script\File\SQLScript.sqlHere’s an example screenshot taken while executing the above script in MySQL Command Line Client program:ģ. ) Or if you are using MySQL Command Line Client program, save the above script into a file, let’s say, SQLScript.sql and execute the following command: `user_id` int(11) NOT NULL AUTO_INCREMENT, Creating a sample MySQL databaseLet’s create a MySQL database called SampleDB with one table Users with the following structure: Execute the following SQL script inside MySQL Workbench: create database SampleDB Extract the zip archive and put the mysql-connector-java-VERSION-bin.jar file into classpath (in a same folder as your Java source files).Ģ. JDBC Driver for MySQL ( download MySQL Connector/J 5.1.25).You may also want to download MySQL Workbench - a graphical tool for working with MySQL databases. MySQL ( download MySQL Community Server 5.6.12).PrerequisitesTo begin, make sure you have the following pieces of software installed on your computer: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |