Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package mydb;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18. *
  19. * @author user
  20. */
  21. public class Db1 {
  22. Connection con = null ;
  23. public void connect(){
  24. try {
  25. Class.forName("com.mysql.jdbc.Driver");
  26. con = DriverManager.getConnection("jdbc:mysql://localhost/dbname", "root", "password");
  27.  
  28. } catch (ClassNotFoundException ex) {
  29. Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex);
  30. } catch (SQLException ex) {
  31. Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex);
  32. }
  33. }
  34. public ArrayList<TUser> getUser(){
  35. ArrayList<TUser> lst = new ArrayList<TUser>();
  36. Statement stmt = null ;
  37. try {
  38. stmt = this.con.createStatement();
  39. ResultSet rs = stmt.executeQuery("select * from table_user limit 1 ,20 ");
  40. while (rs.next()){
  41. TUser u = new TUser(rs);
  42. lst.add(u);
  43. }
  44. } catch (SQLException ex) {
  45. Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex);
  46. }
  47. return lst ;
  48. }
  49.  
  50. public static void main(String[]args ){
  51. System.out.println("main test unit");
  52. Db1 db1 = new Db1();
  53. db1.connect();
  54. ArrayList<TUser> lst = db1.getUser();
  55. for(TUser user : lst){
  56. System.out.println(user.data1 + "\t" + user.fullname );
  57. }
  58. System.out.println("job finished");
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement