從數據庫表中檢索所有行20.5.4.Retrieving All Rows from a Database Table

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class AllRowsDatabase {


	public static void main(String[] args) throws Exception{
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/bookshop";
		String user = "root";
		String password = "";
		Class.forName(driver).newInstance();

		Connection con = DriverManager.getConnection(url,user,password);
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery("select * from book");
		while(rs.next()){
			String isbn = rs.getString("ISBN");
			String name = rs.getString("name");
			String price = rs.getString("price");
			String author = rs.getString("author");
			String publisherID = rs.getString("publisherID");
			System.out.println(isbn + "\t\t" + name + "\t\t"+ price + "\t\t"+ author + "\t\t"+ publisherID);
		}
		con.close();
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章