searching in database using java

Solutions on MaxInterview for searching in database using java by the best coders in the world

showing results for - "searching in database using java"
Giulio
29 Oct 2020
1import java.sql.*;
2
3public class jdbcConn {
4   public static void main(String[] args) throws Exception {
5      Class.forName("org.apache.derby.jdbc.ClientDriver");
6      Connection con = DriverManager.getConnection(
7         "jdbc:derby://localhost:1527/testDb","username", "password");
8      
9      Statement stmt = con.createStatement();
10      String query[] = {
11         "SELECT * FROM emp where id = 1", 
12         "select name from emp where name like 'ravi_'", 
13         "select name from emp where name like 'ravi%'" 
14      };
15      
16      for(String q : query) {
17         ResultSet rs = stmt.executeQuery(q);
18         System.out.println("Names for query "+q+" are");
19         
20         while (rs.next()) {
21            String name = rs.getString("name");
22            System.out.print(name+"  ");
23         }
24         System.out.println();
25      }
26   }
27}