Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. Install MySql server in your machine
  2. Create the database as "test"
  3. create new table as "newtable"
  4.  
  5. Java Project
  6. -------------
  7. - create a java project
  8. - Download the mysql java library "mysql-connector-java-5.1.23-bin.jar"
  9. - Add this jar into project library
  10. - Then copy the below code into the java class
  11.  
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.ResultSet;
  15. import java.sql.Statement;
  16.  
  17. public class Main {
  18.  
  19. public static void main(String args[]){
  20. Connection connection = null;
  21. ResultSet resultset = null;
  22. Statement statement = null;
  23.  
  24. try{
  25. Class.forName("com.mysql.jdbc.Driver").newInstance();
  26. String connectionUrl = "jdbc:mysql://localhost:3306/test";
  27. String connectionUser = "root";
  28. String connectionPassword = "welcome123";
  29. connection = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
  30. statement = connection.createStatement();
  31. resultset = statement.executeQuery("SELECT * FROM newtable");
  32.  
  33. while (resultset.next()) {
  34. int id = resultset.getInt("id");
  35. String name = resultset.getString("name");
  36. System.out.println("ID: " + id + ", Name: " + name);
  37. }
  38. }catch(Exception e){
  39. }finally {
  40. try{
  41.  
  42. if(connection != null)
  43. connection.close();
  44.  
  45. if(statement != null)
  46. statement.close();
  47.  
  48. if(resultset != null)
  49. resultset.close();
  50.  
  51. }catch(Exception ie){
  52.  
  53. }
  54. }
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement