请编写程序,连接数据库,并将该表中的所有条目,按照学号,姓名和生日的顺序输出到控制台。

假设本地机器中装有mysql数据库,该数据库的root密码为“111111”,该数据库中有一张名为“user”的学生信息表,该表包含3个字段,分别为INT型的id(学号),VARCHAR型的NAME(姓名),以及DATE型birthday(生日)。请编写程序,连接数据库,并将该表中的所有条目,按照学号,姓名和生日的顺序输出到控制台。

import java.sql.*;

public class text16 {
    
    
	public static void main(String[] args) throws SQLException {
    
    
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try{
    
     
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localname:3306/test" ;    
		    String username = "root" ;   
		    String password = "111111" ;
		    conn=DriverManager.getConnection(url, username, password);
		    stmt=conn.createStatement();
		    String sql="select * from user";
		    rs=stmt.executeQuery(sql);
		    System.out.println("id  |  NAME  |  brithday  ");
		    while(rs.next()) {
    
    
		    	int id=rs.getInt("id");
		    	String name=rs.getString("name");
		    	Date brithday=rs.getDate("brithday");
		    	System.out.println("id  |  NAME  |  brithday  ");
		    }
		}
		catch (Exception e) {
    
    
			e.printStackTrace();
		}finally {
    
    
			if(rs !=null) {
    
    rs.close();}
			if(stmt !=null) {
    
    stmt.close();}
			if(conn !=null) {
    
    conn.close();}

		}
	}
}

猜你喜欢

转载自blog.csdn.net/snubsa/article/details/111568387