mysql数据库__Jdbc直接操作__Statement__select

一、代码如下

package www.tanniu.jdbc;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class Jdbc {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		java.sql.PreparedStatement ps= null;
		java.sql.Connection cn= null;
		ResultSet rs= null;
		
		try {
			//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			cn= DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?user=root&password=&useUnicode=true&characterEncoding=UTF8");
			ps= cn.prepareStatement("select * from user");
			
			rs= ps.executeQuery();
			
			//rs.next();
			//System.out.println(rs.getInt(1));
			while(rs.next()) {
				String name= rs.getString(2);
				String passwd= rs.getString(3);
				String phone= rs.getString(4);
				String email= rs.getString(5);
				System.out.println(name + " " + passwd + " "+ phone + " " + email);
				//System.out.println(name);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {rs.close();cn.close();} catch (SQLException e) {e.printStackTrace();}
		}
	}

}

 

posted @ 2017-10-08 12:21  18513757531  阅读(494)  评论(0编辑  收藏  举报