1. 程式人生 > >JDBC之Select方法

JDBC之Select方法

建立4個類:StuInfo(實體類)、DBConn(連線、關閉資料庫)、DBUtil(資料庫操作)、RunMain(測試類)

1.StuInfo實體類:

package com.lykion;

public class StuInfo {

	private String sno;
	private String sname;
	private String dname;
	private String ssex;
	private int cno;
	private double mark;
	private String type; 
	
	public StuInfo() {
		
	}
	
	public StuInfo(String sno, String sname, String dname, String ssex, int cno, double mark, String type) {
		super();
		this.sno = sno;
		this.sname = sname;
		this.dname = dname;
		this.ssex = ssex;
		this.cno = cno;
		this.mark = mark;
		this.type = type;
	}
	
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	
	public String getSsex() {
		return ssex;
	}
	public void setSsex(String ssex) {
		this.ssex = ssex;
	}
	
	public int getCno() {
		return cno;
	}
	public void setCno(int cno) {
		this.cno = cno;
	}
	
	public double getMark() {
		return mark;
	}
	public void setMark(double mark) {
		this.mark = mark;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
}

2.DBConn類,對資料庫的操作:開啟資料庫(連線資料庫),操作完成後關閉資料庫,釋放資源
package com.lykion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConn {

	private static final String url = "jdbc:mysql://localhost:3306/test";		//資料庫地址
	private static final String username = "root";		//資料庫使用者名稱
	private static final String password = "123456";		//資料庫密碼
	private static final String driver = "com.mysql.jdbc.Driver";		//mysql驅動
	private static final Connection conn = null;
	
	/**
	 * 連線資料庫
	 * @return
	 */
	public static Connection conn() {
		Connection conn = null;
		try {
			Class.forName(driver);  //載入資料庫驅動
			try {
				conn = DriverManager.getConnection(url, username, password);  //連線資料庫
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 關閉資料庫連結
	 * @return
	 */
	public static void close() {
		if(conn != null) {
			try {
				conn.close();  //關閉資料庫連結
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

3.DBUtil類:主要是資料常用的操作,例項中主要是實現Select操作

package com.lykion;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class DBUtil {

	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;
	private static final CallableStatement cs = null;
	
	/**
	 * 把查詢到的結果放入ResultSet
	 * 通過迭代的方法去讀取結果集中的查詢結果
	 * 輸出查詢結果
	 */
	public static void Select() {
		conn = DBConn.conn();		//呼叫 DBconnection 類的 conn() 方法連線資料庫
		String sql = "SELECT * FROM student01";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();		//將查詢的結果放入ResultSet結果集中
			
			/**
			 * 從結果集ResultSet中迭代取出查詢結果並輸出
			 */
			while(rs.next()) {
				String sno = rs.getString("sno");
				String sname = rs.getString("sname");
				String dname = rs.getString("dname");
				String ssex = rs.getString("ssex");
				int cno = rs.getInt("cno");
				double mark = rs.getDouble("mark");
				String type = rs.getString("type");
				
				System.out.println("學號:"+sno +", "+ "姓名:"+sname +", "+ "所在系:"+dname+", "+ "性別:"+ssex+", "+ "所在系:"+cno +", "+ "成績:"+mark +", "+ "型別:"+type);
			}
		} catch (SQLException e) {
			System.out.println("操作失敗o(╥﹏╥");
			e.printStackTrace();
		}finally {
			DBConn.close();
		}
	}
}


4.RunMain類:測試類,測試DBUtil中的方式是否實現
package com.lykion;

public class RunMain {

	public static void main(String[] args) {
		DBUtil.Select();
	}
}