How to use statement in java | Java Statement tutorial | JDBC
Programming Guru
How to use statement in Java JDBC Statement interface defines the methods and properties to enable send SQL commands to MySQL database and retrieve data from the database. Statement is used for general-purpose access to your database. It is useful when you are using static SQL statements at runtime. The Statement interface cannot accept parameters. Before you can use a Statement object to execute a SQL statement, you need to create one using the Connection object's createStatement( ) method Once you've created a Statement object, you can then use it to execute an SQL statement with one of its three execute methods. boolean execute (String SQL) − Returns a boolean value of true if a ResultSet object can be retrieved; otherwise, it returns false. Use this method to execute SQL DDL statements or when you need to use truly dynamic SQL.
int executeUpdate (String SQL) − Returns the number of rows affected by the execution of the SQL statement. Use this method to execute SQL statements for which you expect to get a number of rows affected - for example, an INSERT, UPDATE, or DELETE statement.
ResultSet executeQuery (String SQL) − Returns a ResultSet object. Use this method when you expect to get a result set, as you would with a SELECT statement.
Code goes here----------------------------------------------------------------------------------------------------------------------------------------------------
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here
final String query = "Select * from employee";
final String update_query = "Update employee set edesignation = 'Manager' where eno = 60";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/testdb","root","");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
String record = rs.getInt("eno")+ " "+rs.getString("ename")+" "+rs.getString("edesignation");
JOptionPane.showMessageDialog(rootPane, record);
}
} catch (SQLException ex) {
Logger.getLogger(JdbcForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
Closing Statement Object
Just as you close a Connection object to save database resources, for the same reason you should also close the Statement object.
A simple call to the close() method will do the job. If you close the Connection object first, it will close the Statement object as well. However, you should always explicitly close the Statement object to ensure proper cleanup.
Types of Statements in JDBC The statement interface is used to create SQL basic statements in Java it provides methods to execute queries with the database. There are different types of statements that are used in JDBC as follows:
Create Statement
Prepared Statement
Callable Statement
- Create a Statement: From the connection interface, you can create the object for this interface. It is generally used for general–purpose access to databases and is useful while using static SQL statements at runtime.
Programming Guru,Guru Programming,programming,gurru,programminggurru,Jdbc mysql,Jdbc in java,Jdbc Statements,statements in java,Java & MySQL - Statement,How to use statement in java Mysql using Jdbc,simple Statement example in jdbc,Java Jdbc,Types of Statements in JDBC,statements in jdbc,jdbc statement,Jdbc,using statement in jdbc
Follow my Facebook Page : https://www.facebook.com/105940115222549 Follow me on Instagram : https://www.instagram.com/p/CViUlw2sOMi Follow me on tumblr : http://programming-guru.tumblr.com Follow me on reddit : https://www.reddit.com/u/Programming_guru1?utm_medium=android_app&utm_source=share ... https://www.youtube.com/watch?v=bLfVuj84Nro
38635967 Bytes