1. 程式人生 > >jdbc簡單查詢

jdbc簡單查詢

jdbc :

注意: URL=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8 ,

需要新增?characterEncoding=utf8 ,暫不清楚原因

對查詢所有沒有問題,但安username查詢,則查詢不到資料


	public static void main(String[] args) {
		Connection connect = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultset = null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			connect =  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","123456");
			String sql ="select id ,username from user where username=? " ;
			preparedStatement = connect.prepareStatement(sql);
			preparedStatement.setString(1, "張三");
			resultset = preparedStatement.executeQuery();
			while(resultset.next()){
				System.out.println("id:"+resultset.getString("id")+"username:"+resultset.getString("username"));
			}
			System.out.println("========end============");
		}catch(Exception  e){
			e.printStackTrace();
		}
	}

結果: