【SSM_Mybatis】学習ノート01

1483 ワード


一、JDBCを使用してデータベースを操作する
1、ドライバパッケージmysql-connective-java-5.0をインポートする.7
2、三大オブジェクト:接続オブジェクト、クエリーオブジェクト、結果セット
3、操作フロー:
データベースドライバのロード
接続の取得
statementの取得
クエリ出力結果{{くえり:しゅつりょくけっか}}
接続を逆順序で閉じる
public class JDBCTest {
	public static void main(String[] args) throws SQLException {
		//    
		Connection connection = null;
		//    
		PreparedStatement pStatement = null;
		//   
		ResultSet rSet = null;
	
		try {
			//       
			Class.forName("com.mysql.jdbc.Driver");
			//    
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssm_mybatis", "root", "root");
			//  statement
			String sql = "select * from user where u_sex=?"; //?     
			pStatement = connection.prepareStatement(sql);
			pStatement.setString(1, "0");
			//       
			rSet = pStatement.executeQuery();
			while(rSet.next()) {
				System.out.println(rSet.getString("u_id")+"  "+rSet.getString("u_username")+"  "+rSet.getString("u_sex"));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//    
			if(rSet!=null) rSet.close();
			if(pStatement!=null) pStatement.close();
			if(connection!=null) connection.close();
		}
		
	}
}

 
二、JDBCに存在する問題.
データベースを操作する際、頻繁に接続を取得し、接続を解放する必要があり、リソースを消費する
検索結果は、遍歴が必要で不便です.
sqlは接続プログラムの中であるいはプレースホルダのようで、ハードコーディング、比較的にデッドボードです