Guest User

sql

a guest
Jul 5th, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import java.sql.SQLException;
  6.  
  7. class JDBCTest {
  8.  
  9. private static String user =”root”;
  10. private static String password =123456;
  11. private static Connection con=null;
  12. private static Statement st=null;
  13. private static ResultSet rs=null;
  14.  
  15. public static void main(String args[])throws Exception {
  16. try {
  17. Class.forName(“com.mysql.jdbc.Driver);
  18. con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbdbdbd”, user, password);
  19.  
  20. }
  21.  
  22. catch (Exception e) {
  23. System.out.println(“e”);
  24. }
  25.  
  26. try{
  27.  
  28. st=con.createStatement();
  29. rs=st.executeQuery(“select * from student”);
  30.  
  31. while(rs.next()) {
  32. System.out.println(rs.getString(“Last_Name”));
  33. }
  34.  
  35. }
  36.  
  37. finally {
  38. try {
  39. if (rs != null) {
  40. rs.close();
  41. }
  42. if (st != null) {
  43. st.close();
  44. }
  45. if (con != null) {
  46. con.close();
  47. }
  48.  
  49. }
  50. catch (SQLException e) {
  51. System.out.println(e);
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58. }
Add Comment
Please, Sign In to add comment