[JAVA] JDBC promises and examples of how to write

JDBC is a Java API that can perform database processing (SQL statements). There are java.sql (core: J2SE), javax.sql (standard extension: J2EE) and so on.

Below is an example using java.sql.

An example of executing a SQL statement in a fixed manner and displaying the result

import java.sql.*;

public class class name{
   public static void main(String args[])
      throws SQLException, ClassNotFoundException { //Exception handling For displaying error pages

      String drv = "oracle.jdbc.OracleDriver"; // Oracle JDBC (Type4)Specify
      String url = "jdbc:oracle:thin:@localhost:1521:Database name"; //Database url

      Class.forName(drv); //Load the driver
      Connection con =  DriverManager.getConnection(url,User name,password); //Connect to database


      Statement stmt = con.createStatement(); //Preparing for inquiries
      String qry = "SQL statement for example SELECT*FROM table name"; //Query preparation
      ResultSet rs = stmt.executeQuery(qry); //Execute SQL statement and receive result as ResultSet


      //ResltSet data income line by line
      while(rs.next()){ // next()Specify each row in the method and return false when the table is finished.
String Column name 1= rs.getString("Column name 1");
Int column name 2= rs.getInt("Column name");
         System.out.println(Column name 1+ "\t" +Column name 2); //display
      }

      //Close connection with database
      rs.close();
      stmt.close();
      con.close();
   }
}

Example of receiving input and executing SQL

If you want to receive input and insert it into SQL statement, the notation such as preparation of SQL statement changes.

import java.sql.*;

public class class name{
   public static void main(String args[])
      throws SQLException, ClassNotFoundException { //Exception handling For displaying error pages

      String drv = "oracle.jdbc.OracleDriver"; // Oracle JDBC (Type4)Specify
      String url = "jdbc:oracle:thin:@localhost:1521:Database name"; //Database url

      Class.forName(drv); //Load the driver
      Connection con =  DriverManager.getConnection(url,User name,password); //Connect to database


      String qry = "SQL statement for example SELECT*FROM table name where column name= ?AND column name= ?";
                               // ?The input value is inserted in the part of. 1 from the left,Specify with 2 and a number.
      PreparedStatement ps = con.prepareStatement(qry); //Preparing for inquiries
      ps.setString(1,Input 1); //1st?Insert input into. If the input is a string.
      ps.setInt(2,Input 2); //Second?Insert input into. If the input is an integer type.
      ResultSet rs = ps.executeQuery(); //Execute SQL statement and receive result as ResultSet


      //ResltSet data income line by line
      while(rs.next()){ // next()Specify each row in the method and return false when the table is finished.
String Column name 1= rs.getString("Column name 1");
Int column name 2= rs.getInt("Column name");
         System.out.println(Column name 1+ "\t" +Column name 2); //display
      }

      //Close connection with database
      rs.close();
      ps.close();
      con.close();
   }
}

Recommended Posts

JDBC promises and examples of how to write
[Java] Types of comments and how to write them
Summary of how to write annotation arguments
[Java] How to output and write files!
How to write and explain Dockerfile, docker-compose
Basics of Java development ~ How to write programs (variables and types) ~
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
Basics of Java development ~ How to write a program (flow and conditional branching) ~
How to write Scala from the perspective of Java
How to write good code
Bit Tetris (how to write)
How to write java comments
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
How to write offline real time Implementation example by ruby and C99 of F04
Comparison of how to write Callback function (Java, JavaScript, Ruby)
How to write and notes when migrating from VB to JAVA
How to check the extension and size of uploaded files
How to deal with different versions of rbenv and Ruby
How to use StringBurrer and Arrays.toString.
Vuze plugin to write and enjoy
How to use EventBus3 and ThreadMode
Studying Java # 6 (How to write blocks)
How to use Spring Data JDBC
[Rails] How to write in Japanese
How to make a JDBC driver
How to call classes and methods
Baseball ball count (how to write)
How to use equality and equality (how to use equals)
How to use setDefaultCloseOperation () of JFrame
How to write a ternary operator
How to connect Heroku and Sequel
How to convert LocalDate and Timestamp
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
[Rails] How to get the URL of the transition source and redirect
[Docker-compose] How to use unnamed and named volumes of volumes. Bind mount
[Swift5] How to get an array and the complement of arrays
How to set the IP address and host name of CentOS8
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
Offline real-time how to write F03 ruby and C implementation example
How to delete large amounts of data in Rails and concerns
How to Synchronize Customer Data Between Salesforce and Kintone (Java-Simple JDBC)
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
How to write offline real-time Java implementation example of F01 problem
How to use OrientJS and OrientDB together
How to name variables 7 selections of discomfort