JDBC練習——數據的增刪查改

前提說明

    前提:java可成功連接數據庫

    SQL Srver

    數據庫: Text

    表:t_student

    表格內容:

    Eclipse

    程序結構:

        說明:MainText.java爲在主函數測試所用,MyText.java爲用Junit直接測試方法所用,兩種方式皆可

程序實現

  JDBCUtil.java

        ①直接方式

package MyJDBCUtil;

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


public class JDBCUtil {
	static String url = null;
	static String user = null;
	static String password = null;
	/**
	 * 獲取連接對象
	 */
	public static  Connection getConn() {
		Connection connection  = null;
		java.sql.DriverManager.registerDriver(new SQLServerDriver()); 
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			url = "jdbc:sqlserver://localhost:1433;DatabaseName=text01";
			user = "sa";
			password = "172228";
			//參數一:協議+訪問的數據庫;參數二:用戶名;參數三:密碼
			connection = DriverManager.getConnection(url,user,password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
	/**
	 * 釋放資源
	 */
	public static void release(Connection connection ,Statement st, ResultSet rs) {
		closeRs(rs);
		closeSt(st);
		closeConn(connection);
	}
		
	private static void closeRs(ResultSet rs) {
		try {
			if(rs != null) {
				rs.close();
			}
			rs = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static void closeSt(Statement st) {
		try {
			if(st != null) {
				st.close();
			}
			st = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static void closeConn(Connection connection) {
		try {
			if(connection != null) {
				connection.close();
			}
			connection = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

        ②配置文件方式

              注意:這裏配置文件(jdbc.properties)是放在src下的

package MyJDBCUtil;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil {
	static String driverClass = null;
	static String url = null;
	static String user = null;
	static String password = null;
	
	static {
		try {
			//創建一個屬性配置文件
			Properties properties = new Properties();
			//InputStream is = new FileInputStream("jdbc.properties");
			InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");//使用類加載器去讀取src底下的資源文件
			//導入輸入流
			properties.load(is);
			//讀取屬性
			driverClass = properties.getProperty("driverClass");
			url = properties.getProperty("url");
			user = properties.getProperty("user");
			password = properties.getProperty("password");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 獲取連接對象
	 */
	public static  Connection getConn() {
		Connection connection  = null;
		java.sql.DriverManager.registerDriver(new SQLServerDriver()); 
		try {
			Class.forName(driverClass);
			//參數一:協議+訪問的數據庫;參數二:用戶名;參數三:密碼
			connection = DriverManager.getConnection(url,user,password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	/**
	 * 釋放資源
	 */
	public static void release(Connection connection ,Statement st, ResultSet rs) {
		closeRs(rs);
		closeSt(st);
		closeConn(connection);
	}
		
	private static void closeRs(ResultSet rs) {
		try {
			if(rs != null) {
				rs.close();
			}
			rs = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static void closeSt(Statement st) {
		try {
			if(st != null) {
				st.close();
			}
			st = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static void closeConn(Connection connection) {
		try {
			if(connection != null) {
				connection.close();
			}
			connection = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

        jdbc.properties

driverClass = com.microsoft.sqlserver.jdbc.SQLServerDriver
url = jdbc:sqlserver://localhost:1433;DatabaseName=text01
user = sa
password = 172228

   TextMain.java / MyText.java(裏面的方法)

      查找數據

        SQL語句:

SELECT *

FROM t_student ;

        實現方法:

public void textSelect() {

		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		//查詢
		try {
			//獲取連接對象
			conn = JDBCUtil.getConn();
			//根據連接對象,得到statement
			st = conn.createStatement();
			//執行SQL語句,返回ResultSet
			String sql = "select * from t_student";
			rs =st.executeQuery(sql);	
			//便來結果集
			while(rs.next()) {
				String name = rs.getString("name");
				int age = rs.getInt("age");
				System.out.println(name+"    "+age);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.release(conn, st, rs);
		}		
	}

      插入數據

        SQL語句:

INSERT *

INTO t_student

VALUES(007,'papa','20') ;

        實現方法:

public void textInsert() {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		//查詢
		
		try {
			//獲取連接對象
			conn = JDBCUtil.getConn();
			//根據連接對象,得到statement
			st = conn.createStatement();
			//執行添加
			String sql = "insert into t_student values(007,'papa','20')";
			int result = st.executeUpdate(sql);
			if(result > 0) {
				System.out.println("添加成功");
			}
			else {
				System.out.println("添加失敗");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

      刪除數據

        SQL語句:

DELETE *

FROM t_student

WHERE name ='papa' ;

        實現方法:

public void textDelete() {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		//查詢
		
		try {
			//獲取連接對象
			conn = JDBCUtil.getConn();
			//根據連接對象,得到statement
			st = conn.createStatement();
			//執行添加
			String sql = "delete from t_student where name ='papa'";//滿足條件的全部刪除
			int result = st.executeUpdate(sql);
			if(result > 0) {
				System.out.println("刪除成功");
			}
			else {
				System.out.println("刪除失敗");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

      修改數據

        SQL語句:

UPDATE t_student

SET age = 21

WHERE name ='bubu' ;

        實現方法:

public void textUpdate() {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		//查詢
		
		try {
			//獲取連接對象
			conn = JDBCUtil.getConn();
			//根據連接對象,得到statement
			st = conn.createStatement();
			//執行添加
			String sql = "update t_student set age = 21 where name ='papa'";//滿足條件的全部修改
			int result = st.executeUpdate(sql);
			if(result > 0) {
				System.out.println("修改成功");
			}
			else {
				System.out.println("修改失敗");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

結果

    查詢方法執行結果:

   插入方法執行結果:

 

    刪除方法執行結果:

  修改方法執行結果:

 

 

 

 

警告出現的原因我還沒有搞清楚,先寫在這裏,之後弄清楚了再修改。

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