Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. package lab2;
  2.  
  3. import java.sql.*;
  4. import java.util.Scanner;
  5. public class JavaMysqlSelectExample {
  6. public static void main(String[] args) {
  7.  
  8. Scanner sc = new Scanner(System.in);
  9. float heightInput = 0.0f;
  10. heightInput = sc.nextFloat();
  11.  
  12. try
  13. {
  14. // create the mysql database connection
  15. String myDriver = "com.mysql.jdbc.Driver";
  16.  
  17. //specify the database to connect
  18. String myUrl = "jdbc:mysql://localhost/studentcontact";
  19.  
  20. //connect to the database
  21. Class.forName(myDriver);
  22. Connection conn = DriverManager.getConnection(myUrl, "root", "");
  23.  
  24. // create the SQL select statement.
  25. String query = "SELECT * FROM student";
  26.  
  27. // create the java statement
  28. Statement st = conn.createStatement();
  29.  
  30. // execute the query; the result of the execution is stored in an object of ResultSet class
  31. ResultSet rs = st.executeQuery(query);
  32.  
  33. // there can be more than one result. so, we have to go through each data. in each round, only one record will be readed.
  34.  
  35. while (rs.next())
  36. {
  37. // int id = rs.getInt("studentId");
  38. String firstName = rs.getString("firstname");
  39. String lastName = rs.getString("lastname");
  40. // int age = rs.getInt("age");
  41. float height = heightInput;
  42. height = rs.getInt("height");
  43.  
  44.  
  45. // print the results
  46. // System.out.format("%s, %s, %s, %s, %s\n", id, firstName, lastName, age, height);
  47. // System.out.format("%s\n", id);
  48. // System.out.format("%s\n", firstName);
  49. // System.out.format("%s, %s, %s, %s\n", firstName, lastName, age, height);
  50. if(rs.getInt("height") == heightInput){
  51. System.out.format("%s, %s, %s\n", firstName, lastName, heightInput);
  52.  
  53. }
  54.  
  55. }
  56.  
  57. st.close();
  58. }
  59. catch (Exception e)
  60. {
  61. System.err.println("Got an exception! ");
  62. System.err.println(e.getMessage());
  63. e.printStackTrace();
  64. }
  65.  
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement