how to write queries in java

Solutions on MaxInterview for how to write queries in java by the best coders in the world

showing results for - "how to write queries in java"
Yanis
12 Mar 2016
1import java.sql.*;
2
3/**
4 * A Java MySQL SELECT statement example.
5 * Demonstrates the use of a SQL SELECT statement against a
6 * MySQL database, called from a Java program.
7 * 
8 * Created by Alvin Alexander, http://alvinalexander.com
9 */
10public class JavaMysqlSelectExample
11{
12
13  public static void main(String[] args)
14  {
15    try
16    {
17      // create our mysql database connection
18      String myDriver = "org.gjt.mm.mysql.Driver";
19      String myUrl = "jdbc:mysql://localhost/test";
20      Class.forName(myDriver);
21      Connection conn = DriverManager.getConnection(myUrl, "root", "");
22      
23      // our SQL SELECT query. 
24      // if you only need a few columns, specify them by name instead of using "*"
25      String query = "SELECT * FROM users";
26
27      // create the java statement
28      Statement st = conn.createStatement();
29      
30      // execute the query, and get a java resultset
31      ResultSet rs = st.executeQuery(query);
32      
33      // iterate through the java resultset
34      while (rs.next())
35      {
36        int id = rs.getInt("id");
37        String firstName = rs.getString("first_name");
38        String lastName = rs.getString("last_name");
39        Date dateCreated = rs.getDate("date_created");
40        boolean isAdmin = rs.getBoolean("is_admin");
41        int numPoints = rs.getInt("num_points");
42        
43        // print the results
44        System.out.format("%s, %s, %s, %s, %s, %s\n", id, firstName, lastName, dateCreated, isAdmin, numPoints);
45      }
46      st.close();
47    }
48    catch (Exception e)
49    {
50      System.err.println("Got an exception! ");
51      System.err.println(e.getMessage());
52    }
53  }
54}
55
Ulrich
31 Jan 2021
12, Fred, Flinstone, 2010-06-23, false, 6000
23, Barney, Rubble, 2010-06-23, false, 5000
3
queries leading to this page
how do i querie in javahow to fetch data from mysql database in javahow to read from database in javajdbc query with 3fgrab all mysql javagrab varchar from sql in javaquery java exemple javaselect 2afrom sql where javasql query javahow to write a sql statement in javaget data from mysql javajava read from databasejava sql select statementjdbc queries in javaselect values database jvaget the sql code from query javajava get data from mysqlhow should you make queries to sql in javajdbc query one linehow to get a string from a database in sql in java select jdbc sqlsql java query 25sselect data into table java mysqlselect statement sql javajava jdbc queryjava program to fetch data from mysql databasejava read data from mysqljava jdbc sql query database connection and run query javawriting sql queries in javaexecutequery db query in javajava sql query exampleselect id from company where name 3d 3f in java mysqlhow to write sql query in javamaking sql queries in javause sql query in javahow to write query in javajava mysql findhow to make sql query via javaselect query in jdbchow to create queries in javasql query in javafetch data from mysqljavamysql query javajava progra to execute sql queressql queries in javawriting select statements in javasql query in statement javajdbc example select fromjava connect to mysql selectjava mysql read tablemysql java selectselect sql javajava get string from mysql execute sql query to get users in database javasql statement java example likehow to query mysql in javacall a sql statement in javaread data from mysql database using javahow to use select sql in javajava sql queryfind query in jdbcjdbc queryjdbc run queryjdbc query with 3dselect query with jdbc javajava querieshow to make select in javasql seaech in javaselect 2a from table sql javahow to read data from mysql table using javasleect stament java jdbcjava select query wherejava send custom sql commands to dbjava mysql execute queryselectquery string example javahow to execute show database query in javajava query examplejava mysql select lisrtjava simple sql queryselect in sql in javajava mysql select query examplejava select from where mysqlhow to print response of sql select query in javajava make a sql requestseql calls in javajava mysql queryjdbc make queryjdbc oracle select examplejdbc select statementgrab data from mysql javajava mysql select querymysql get data from table javajava select jdbcjava statement selectshow java sql queryjava class to fetch data fro mysql databasefetch data from mysql table javawhat are queries in javaselect 2a from 28select 3f 2c 3f 29 in java sqlhow to write sql queries in javause database and select java mysqlhow to execute mysql query in javajava mysql select data in rowhow to fetch values from mysql db with java 40query select in javsdisplay database values in java using mysql connector selectsql java queryjava sql select fromsql get data javahow to get all data from a database in javaselect data into table java mydqljava mysql selectjava query mysqloutput results of sql select javajava sql query in statementfind data in sql on javajava select all from databasejava sts print query resultjava mysqlp where querymysql java select queryselect jdbc javahow to write queries in java