Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mydb;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author user
- */
- public class Db1 {
- Connection con = null ;
- public void connect(){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost/dbname", "root", "password");
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public ArrayList<TUser> getUser(){
- ArrayList<TUser> lst = new ArrayList<TUser>();
- Statement stmt = null ;
- try {
- stmt = this.con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from table_user limit 1 ,20 ");
- while (rs.next()){
- TUser u = new TUser(rs);
- lst.add(u);
- }
- } catch (SQLException ex) {
- Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex);
- }
- return lst ;
- }
- public static void main(String[]args ){
- System.out.println("main test unit");
- Db1 db1 = new Db1();
- db1.connect();
- ArrayList<TUser> lst = db1.getUser();
- for(TUser user : lst){
- System.out.println(user.data1 + "\t" + user.fullname );
- }
- System.out.println("job finished");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement