• jdbc的ORM映射类与表结构


    package com.commns.demo;
    @SuppressWarnings("all")
    public class User {
    
    	private Integer id;
    	private String name;
    	private Integer age;
    	private String address;
    	
    	public User() {
    		super();
    	}
    	@Override
    	public String toString() {
    		return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
    	}
    	public User(Integer id, String name, Integer age, String address) {
    		super();
    		this.id = id;
    		this.name = name;
    		this.age = age;
    		this.address = address;
    	}
    	public Integer getId() {
    		return id;
    	}
    	public void setId(Integer id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	public String getAddress() {
    		return address;
    	}
    	public void setAddress(String address) {
    		this.address = address;
    	}
    	
    }
    

      

    package com.commns.demo;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestUser {
    
    	private static final String url="jdbc:mysql://localhost:3306/mybase";
    	private static final String user="root";
    	private static final String password="root";
    	
    	public static void main(String[] args) {
    		Connection con = getConnctions();
    		PreparedStatement ps=null;
    		ResultSet rs=null;
    		List<User> array=new ArrayList<User>();
    		String sql="select * from user";
    		try {
    			 ps = con.prepareStatement(sql);
    			 rs = ps.executeQuery();
    			 while(rs.next()) {
    				 
    				 User user=new User(rs.getInt(1),rs.getString("sname"),rs.getInt(3),rs.getString("adderss"));
    				  array.add(user);
    			 }
    			getLists(array); 
    			
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		close(rs,ps,con);
    	}
    	/**
    	 *打印数据
    	 */
    	private static void getLists(List<User> list) {
    		for (User user : list) {
    //			System.out.println(user);
    			System.out.println(user.getId()+user.getName()+user.getAge()+user.getAddress());
    		}
    		
    	}
    	/**
    	 * 关闭数据库
    	 * @param ac
    	 */
    	public static void close(AutoCloseable... ac) {
    		
    		for(AutoCloseable auto:ac) {
    			if(auto!=null) {
    				try {
    					auto.close();
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    	
    	/**
    	 * 获得jdbc的连接并返回
    	 * @return
    	 */
    	public static Connection getConnctions() {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			return DriverManager.getConnection(url, user, password);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    }
    

      

  • 相关阅读:
    ASCII,Unicode,UTF
    C#值类型和引用类型2
    C#中使用Foreach
    CSS基础(2)
    CSS基础
    HTML基础
    MySQL高级
    MySQL和Python交互案例练习(2)
    MySQL和Python交互案例练习(1)
    外键SQL语句的编写
  • 原文地址:https://www.cnblogs.com/qurui1997/p/10628138.html
Copyright © 2020-2023  润新知